OpenERP @http。路由('demo_json', type= " json ") URL不显示json数据

时间:2023-01-31 20:24:10

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

我在OpenERP框架中创建控制器。下面是我的代码,我设置了http。路线类型=“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.

以上代码在我修改URL http://localhost:8069/demo_html后登录到openerp,显示返回结果时非常完美,这是h1标题标签中的一个测试。

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".

但是,我尝试键入="json",并添加以下json代码,并再次尝试调用URL http://localhost:8069/demo_json,它不能正常工作,并显示错误"内部服务器错误"。

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.

我的问题是如何路由json。如有任何帮助,我们将不胜感激。

2 个解决方案

#1


1  

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

这是因为type="json"和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.

#2


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.

我认为,在使用type=“json”时,您需要做一些额外的工作,您必须使用来自js的json rpc触发该方法。

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"}]

#1


1  

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

这是因为type="json"和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.

#2


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.

我认为,在使用type=“json”时,您需要做一些额外的工作,您必须使用来自js的json rpc触发该方法。

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"}]