You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ODOO 11.0如何返回不带JSON-RPC附加参数的纯JSON对象

解决Odoo 11控制器返回纯JSON(不带JSON-RPC包装)的问题

我来帮你搞定这个问题——你遇到的情况是因为Odoo的type='json'路由默认会遵循JSON-RPC规范自动包装你的返回结果,所以才会带上result、id和jsonrpc这些额外字段。要返回纯JSON对象,你需要调整路由的类型并手动构建HTTP响应:

具体修改步骤:

  • 将路由的type='json'改为type='http',这样Odoo就不会自动用JSON-RPC包装你的返回值;
  • 导入json模块和Odoo的Response类,用来手动序列化字典并构建响应;
  • 把你的响应字典转换成JSON字符串,然后用Response对象返回,同时设置正确的Content-Type头。

修改后的完整代码:

import json
from odoo.http import Controller, route, Response

class YourController(Controller):
    @route('/userappoint/webhook_test/', type='http', auth='public', method=['POST'], csrf=False, website=True)
    def webhook_test(self, **kw):
        response = {
            'speech': 'hello my name is shubham',
            'displayText': 'hello testing',
            'source': 'webhook'
        }
        # 手动构建纯JSON响应
        return Response(
            json.dumps(response),
            content_type='application/json;charset=utf-8',
        )

为什么这样有效?

当路由类型设为http时,Odoo会直接返回你构建的Response对象,不会添加任何JSON-RPC相关的包装字段。通过设置content_type='application/json',客户端能正确识别返回的是纯JSON数据,这样就能得到你想要的结果了。

内容的提问来源于stack exchange,提问作者user3293412

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:13:19