2

I am create controller in OpenERP Framework. Following is my code and i set http.route type="http",

import openerp.http as http
from openerp.http import request

class MyController(http.Controller):

    @http.route('demo_html', type="http")
    def some_html(self):
        return "<h1>This is a test</h1>"

Above code work perfect once i login into openerp after i modify URL http://localhost:8069/demo_html show me return result This is a test in h1 heading tag.

But same way i try to type="json" and add following json code and again try to call URL http://localhost:8069/demo_json Its not work properly and show me error "Internal Server Error".

import openerp.http as http
from openerp.http import request

class MyController(http.Controller):

    @http.route('demo_html', type="http") // Work Pefrect when I call this URL
    def some_html(self):
        return "<h1>This is a test</h1>"

    @http.route('demo_json', type="json") // Not working when I call this URL
    def some_json(self):
        return {"sample_dictionary": "This is a sample JSON dictionary"}

So my question is how to route json. Any help would be appreciate Thank you.

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76

2 Answers2

1

This is because there is difference between type="json" and type="http".

type="json":

it will call JSONRPC as an argument to http.route() so here , there will be only JSON data be able to pass via JSONRPC, It will only accept json data object as argument. 

type="http":

As compred to JSON, http will pass http request arguments to http.route() not json data.
Bazzinga...
  • 996
  • 2
  • 16
  • 26
0

I think , you need to do some extra stuff while working with type="json",you have to trigger that method using json rpc from js.

like :
$(document).ready(function () {
    openerp.jsonRpc("demo_json", 'call', {})
            .then(function (data) {
                $('body').append(data[0]);
            });
    return;
})

and yes do not forget to return your dictionary in list like

@http.route('demo_json', type="json")
def some_json(self):
    return [{"sample_dictionary": "This is a sample JSON dictionary"}]
Heroic
  • 980
  • 4
  • 12
  • Thank you for giving me answer. jquery code call on demo_json page head tag inside i add. another things i as per my knowledge JSON return dictonary format to display on page – Jaykumar Patel Apr 03 '14 at 06:47
  • Yes you are right JSON retunr dictonary ,but it also return list of dictionary , you can check http://jsonlint.com/ and type something like [{}] and validate. – Heroic Apr 03 '14 at 07:06
  • @S͢kyD͢ream your question is old 3 years but today in 2017 I want return Json? How call demo_json from url and where put above jquery? – Pointer Feb 17 '17 at 13:15
  • @Pointer we can not update question base on new Odoo update. If you have question ask here directly contact me. Thanks – Jaykumar Patel Feb 23 '17 at 09:37