OpenERP @ http.route('demo_json',type =“json”)URL不显示JSON数据

时间:2014-04-01 04:35:25

标签: json openerp openerp-8

我在OpenERP Framework中创建控制器。以下是我的代码,我设置 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>"

上面的代码工作完美,一旦我在修改网址http://localhost:8069/demo_html后登录openerp,就会在h1标题标记中显示返回结果This is a test

但同样的方法我尝试type="json"并添加以下json代码并再次尝试调用网址http://localhost:8069/demo_json它无法正常工作并向我显示错误"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"}

所以我的问题是如何路由json 。任何帮助都会很感激谢谢。

2 个答案:

答案 0 :(得分:1)

这是因为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.

答案 1 :(得分:0)

我认为,在使用type =&#34; json&#34;时,你需要做一些额外的事情,你必须使用js中的json rpc来触发该方法。

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

并且不要忘记在列表中返回您的字典

@http.route('demo_json', type="json")
def some_json(self):
    return [{"sample_dictionary": "This is a sample JSON dictionary"}]