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 (将#修改为@)