2

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

一旦我在修改 URL 后登录到 openerp,上面的代码就可以完美运行,显示我在 h1 标题标签中http://localhost:8069/demo_html返回结果。This is a test

但同样我尝试type="json"添加以下 json 代码并再次尝试调用 URLhttp://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。任何帮助将不胜感激谢谢。

4

2 回答 2

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.
于 2014-04-01T06:06:38.230 回答
0

我认为,在使用 type="json" 时,您需要做一些额外的事情,您必须使用来自 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"}]
于 2014-04-03T06:07:43.900 回答