如何在 Odoo 控制器中获取 JSON 数据? [英] How to get JSON data in an Odoo controller?

查看:25
本文介绍了如何在 Odoo 控制器中获取 JSON 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 Odoo 控制器发送一些 JSON 数据,但是当我发送请求时,我总是收到 404 作为响应.

I am trying to send some JSON data to an Odoo controller, but when I send the request, I always get 404 as response.

这是我的控制器的代码:

This is the code of my controller:

import openerp.http as http
import logging
_logger = logging.getLogger(__name__)

class Controller(http.Controller):

    @http.route('/test/result', type='json', auth='public')
    def index(self, **args):
        _logger.info('The controller is called.')
        return '{"response": "OK"}'

现在,我输入 URL (http://localhost:8069/test/result) 在浏览器上检查它是否可用,然后我得到 function index at 0x7f04a28>,/test/result: 函数声明为能够处理 'json' 类型的请求,但调用'http' 类型的请求.这样我就知道控制器正在监听那个 URL 并期待 JSON 数据.

Now, I type the URL (http://localhost:8069/test/result) on the browser to check if it is available, and I get function index at 0x7f04a28>, /test/result: Function declared as capable of handling request of type 'json' but called with a request of type 'http'. This way I know that the controller is listening at that URL and is expecting JSON data.

所以我打开一个 Python 控制台并输入:

So I open a Python console and type:

import json
import requests
data = {'test': 'Hello'}
data_json = json.dumps(data)
r = requests.get('http://localhost:8069/test/result', data=data_json)

当我在控制台中打印 r 时,它返回 ,并且我在日志中看不到任何消息(我期待 调用控制器.).

When I print r in the console, it returns <Response [404]>, and I cannot see any message in the log (I was expecting The controller is called.).

这里有一个类似的问题,但情况并不完全相同:

There is a similar question here, but it is not exactly the same case:

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

谁能帮帮我?我做错了什么?

Can anyone help me? What am I doing wrong?

推荐答案

我刚刚解决了这个问题.

I have just solved the problem.

首先,正如@techsavvy 所说,我必须修改装饰器,编写 type='http' 而不是 type='json'.

Firstly, as @techsavvy told, I had to modify the decorator, to write type='http' instead of type='json'.

之后,来自控制台的请求返回了 404 错误,因为它不知道将数据发送到哪个数据库.在 localhost:8069 我有多个数据库.所以我试图在那个港口只有一个.也就是说,现在效果很好!

And after that, the request from the console returned a 404 error because it did not know which database it was sending data to. In localhost:8069 I had more than one database. So I tried to have only one at that port. And that is, now it works great!

为了在不删除任何其他数据库的情况下进行管理,我刚刚修改了配置文件以更改参数 db_filter 并放置一个仅包含我当前数据库的正则表达式.

To manage that without removing any of the other databases, I have just modified the config file to change the parameter db_filter and put there a regular expression which only included my current database.

这篇关于如何在 Odoo 控制器中获取 JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆