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.

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

you check I'm return JSON data return {"sample_dictionary": "This is a sample JSON dictionary"}
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"}]

4 Comments

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
Yes you are right JSON retunr dictonary ,but it also return list of dictionary , you can check jsonlint.com and type something like [{}] and validate.
@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 we can not update question base on new Odoo update. If you have question ask here directly contact me. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.