微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ionic 5 和 Odoo 连接在处理 json 响应时出错

如何解决ionic 5 和 Odoo 连接在处理 json 响应时出错

我正在尝试使用 api 接口连接 odoo 和 ionic 5。下面是我的 odoo 控制器:

 @http.route('/ionicon/authenpost',type="json",csrf=False,cors="*",auth="public",website=False,method=['POST'])
    def authenPost(self,login,pwd,base_location=None):
        
        env = request.env(context=dict(request.env.context,show_address=False,no_tag_br=True))
        db = env.cr.dbname
        request.session.authenticate(db,pwd)
        return request.env['ir.http'].session_info()

部署没问题,并且已经用 Postman 测试过。反应也不错。

但是当我尝试使用 ionic 5 android 应用程序调用时,出现以下错误

{"status":400,"url":"http://172.20.10.3:8069/ionicon/authenpost","headers":{"date":"Mon,18 Jan 2021 17:01:05 GMT","content-length":"308","server":"Werkzeug/0.16.1 Python/3.8.2","x-android-selected-protocol":"http/1.0","x-android-response-source":"NETWORK 400","x-android-received-millis":"1610989264019","x-android-sent-millis":"1610989263965","content-type":"text/html"},"error":"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p>&lt;function ionic_conController.authenPost at 0x7fc9687620d0&gt;,/ionicon/authenpost: Function declared as capable of handling request of type 'json' but called with a request of type 'http'</p>\n"}

实际上我已经通过了“application/json”标头。请看下面的代码

 this.http.sendRequest("http://172.20.10.3:8069/ionicon/authenpost",{ 
  method: 'post',responseType: 'json',data:{ 'jsonrpc':'2.0','params' : { 'login':'testuser@sample.com','pwd':'testabc' }},headers: {
    accept: 'application/json','Content-Type': 'application/json'
}}).then(data=>{
  console.log(JSON.stringify(data));
}).catch(err =>{
  console.log(JSON.stringify(err));
});

我正在使用

import { HTTP } from '@ionic-native/http/ngx';

它确实适用于 HTTPClient,下面是我的工作代码

const httpHeader_ = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };

    let config ={ headers: new HttpHeaders().set('Content-Type','application/json') };

    this.httpCli.post("http://172.20.10.3:8069/ionicon/authenpost",{jsonrpc : "2.0",params : { 'login':'sampleuser@test.com','pwd':'password123' } },config).subscribe(data => {
      console.log(data);
    },err => {
      console.log(err);
    });

我想知道如何使用 HTTP?

我可以在哪里放置内容类型?

最好的Rgds, jm

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。