How to send a simple message and status as a response in an Odoo JSON controller?

别说谁变了你拦得住时间么 提交于 2019-12-01 01:37:09

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.

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'

Try this for type json

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

Using a request structured like this.

json_data = {"test": True}

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

Use the above for a python request.

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);  
        } 
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!