如何在 Odoo JSON 控制器中发送一条简单的消息和状态作为响应? [英] How to send a simple message and status as a response in an Odoo JSON controller?

查看:31
本文介绍了如何在 Odoo JSON 控制器中发送一条简单的消息和状态作为响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了不同的方法来做到这一点,但都没有奏效.

I tried different ways of doing that, but they didn't work.

首先我尝试了这种方式:

import openerp.http as http
from openerp.http import Response

class ResPartnerController(http.Controller):

    @http.route('/odoo/create_partner', type='json', auth='none')
    def index(self, **kwargs):

    Response.status = '400'
    return "Result message"

我在客户端获得了正确的状态和消息.但是如果我在 Odoo 界面上执行任何操作,我会收到这个奇怪的警告

I get the right status and the message in the client. But I get this strange warning if I do any action on the Odoo interface

有没有办法避免这条消息?

Is there a way to avoid this message?

我也尝试了这两种方式:

data = {'result': 'RESULT message'}
json_data = json.dumps(data, encoding='utf-8')
headers = [('Content-Type', '{}; charset=utf-8'.format('application/json'))]
mimetype = 'application/json'
res = Response(
    response=json_data,
    status=status,
    headers=headers,
    mimetype=mimetype,
)
return res

msg = u'Response 200 badly built, falling back to a simple 200 OK response'
res = Response(msg, status=200)
return res

但我总是收到此错误作为客户端的答案:

But I always get this error as answer in the client:

TypeError: <Response 9 bytes [400 BAD REQUEST]> is not JSON serializable
", "message": "<Response 9 bytes [400 BAD REQUEST]> is not JSON serializable"

那么,是否有一种干净的方式来回答带有响应状态的简单消息?

So, is there a clean way of answer a simple message with the status of the response?

发送响应的状态对我来说也很重要

It is important for me to send the status of the response as well

如果我只是回复一条消息,一切正常,但如何在没有奇怪行为的情况下更改状态?

If I simply respond a message everything works fine, but how to change the status without strange behaviours?

顺便说一下,我用这个脚本来调用

By the way, I use this script to do the calls

# -*- coding: utf-8 -*-

import requests
import json

url = 'http://localhost:8069/odoo/create_partner'
headers = {'Content-Type': 'application/json'}

data_res_partner = {
    'params': {
        'name': 'Example',
        'email': 'jamon@test.com',
    }
}

data_json = json.dumps(data_res_partner)
response = requests.post(url=url, data=data_json, headers=headers)
print(response.status_code)
print(response.content)

更新

最后@Phillip Stack 告诉我用 XML-RPC 来做这件事,所以我写了这个 其他问题 以澄清我的疑虑.

Finally @Phillip Stack told me to do this with XML-RPC, so I wrote this other question in order to clarify my doubts.

推荐答案

试试这个,我不确定我是否理解这里涉及的所有复杂性.尝试一个 vanilla 请求并将响应解析为 json 作为解决方法.如果我弄清楚 json 请求/响应,我会更新它.我遇到了与您类似的问题,但能够使以下内容正常工作.

Try this, I am not sure if I understand all the complexities involved here. Try a vanilla request and parse the response as json as a work around. If I figure out json request/response I will update this. I was having similar issues as yourself but was able to get the following to work.

试试这个类型 http

Try this for type http

 @http.route('/test/test', auth='none', type='http')
 def test(self, **kwargs):
     return Response("TEST",content_type='text/html;charset=utf-8',status=500)

我的请求看起来像这样.

My request looks like this.

r = requests.post("http://localhost:8069/test/test",data={}))    
>>> r
<Response [500]>
>>> r.text
u'TEST'

试试这个 json 类型

Try this for type json

@http.route('/test/test', auth='none', type='json')
def test(self, **kwargs):
    Response.status = '400'
    return {'test':True}

使用这样结构的请求.

json_data = {"test": True}

requests.post("http://localhost:8069/test/test",data=json.dumps(json_data),headers={"Content-Type":"application/json"})

将上述内容用于 Python 请求.

Use the above for a python request.

使用下面的 javascript 示例

Use the example below for javascript

var json_data = { 'test': true };

$.ajax({ 
        type: "POST", 
        url: "/test/test", 
        async: false, 
        data: JSON.stringify(json_data), 
        contentType: "application/json", 
        complete: function (data) { 
            console.log(data);  
        } 
});

这篇关于如何在 Odoo JSON 控制器中发送一条简单的消息和状态作为响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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