【问题标题】:Odoo JSONRPC service (http.route type=json) not workOdoo JSONRPC 服务(http.route type=json)不起作用
【发布时间】:2016-05-13 08:42:06
【问题描述】:

我正在尝试在 odoo 中创建 jsonrpc 服务。当我使用 doc https://www.odoo.com/documentation/8.0/howtos/website.html 创建干净的模块并添加这样的代码时

**[controllers.py]**
@http.route(['/my_academy/ret/'], type='json', auth="public")
    def change_size(self):
        return {'x': 1, 'y': 2}

尝试从js连接:

    "use strict";
    var requestUrl = '/my_academy/ret/';

    openerp.jsonRpc(requestUrl, 'call', {})
        .then(function (data) {
            alert(data['x']);
        });

一切正常。我收到消息“1”。 当我使用 doc https://www.odoo.com/documentation/8.0/howtos/backend.html 创建模块并添加此类代码时

**[controllers.py]**
@http.route(['/academy/jsonrpc/'], type='json', auth="public")
    def return_map(self):
        return {'x': 12345, 'y': 2222}

修改js,尝试连接

    "use strict";
    var requestUrl = '/academy/jsonrpc/';

    openerp.jsonRpc(requestUrl, 'call', {})
        .then(function (data) {
            alert(data['x']);
        });

我收到错误

code: 200
data: Object
    arguments: Array[0]
        length: 0
        __proto__: Array[0]
        ....
    debug: "Traceback (most recent call last):↵  
            File "/home/skif/odoo/openerp/http.py", line 539, in _handle_exception↵    
            return super(JsonRequest, self)._handle_exception(exception)↵
            File "/home/skif/odoo/openerp/addons/base/ir/ir_http.py", line 152, in _dispatch↵    
            rule, arguments = self._find_handler(return_rule=True)↵  
            File "/home/skif/odoo/openerp/addons/base/ir/ir_http.py", line 65, in _find_handler↵    
            return self.routing_map().bind_to_environ(request.httprequest.environ).match(return_rule=return_rule)↵  
            File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1430, in match↵    
            raise NotFound()↵
            NotFound: 404: Not Found↵"
    message: ""
    name: "werkzeug.exceptions.NotFound"
    __proto__: Object

message: "Odoo Server Error"
__proto__:Object

我做错了什么?为什么我不能使用类似的代码?

【问题讨论】:

    标签: openerp odoo-8 json-rpc


    【解决方案1】:

    试试这个代码:

    @http.route(['/academy/jsonrpc'], type='json', auth="public", website=True)
    def return_map(self, **post):
        return {'x': 12345, 'y': 2222}
    

    它可能对你的情况有所帮助。

    【讨论】:

    • 你有没有在 init.py 中导入你的控制器
    • 我尝试使用 auth="none" - 没有。如果为网站开发模块,那么一切正常。如果为后端开发模块,例如 odoo.com/documentation/8.0/howtos/backend.html 我有问题。我可能需要声明任何模块或安全参数吗?
    • 是的,我有。 # -- coding: utf-8 -- import controllers import models #interhits from .进口伙伴
    • 这是代码工作正常 @http.route('/academy/academy/', auth='public', website=True) def index(self, **kw): return http.request .render('academy.index', { 'message': ["Hello World", "Hi all!"] })
    • 但这不是 Json :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多