【问题标题】:Ionic-native HTTP POST method not working on iOS (but perfectly working on Android)Ionic-native HTTP POST 方法在 iOS 上不工作(但在 Android 上完美工作)
【发布时间】:2019-10-09 05:16:27
【问题描述】:

我在我的 Ionic 4 项目中使用 @ionic-native/http 通过 POST 方法发送带有 UserId 和 Password 的正文和带有 'Content-Type': 'application/json' 的标头来登录用户。它在 android 中运行良好,但在 iOS 上它响应 http 400 错误。

Dependencies: 

"@ionic-native/http": "^5.3.0",

"cordova-plugin-advanced-http": "^2.0.9",

我厌倦了使用 @angular/http,但它在浏览器、Android 和 iOS 上出现了 CORS 错误。而且我无法更改服务器端代码以启用 CORS。

代码:

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

// Login method  
async login(userId, password) {
    // login user
    const httpBody = {
      'UserId': userId,
      'Password': btoa(password)
    };
    const httpHeader = {
      'Content-Type':  'application/json',
    };
    const user = await this.http.post(this.loginURL, httpBody, httpHeader);
    return JSON.parse(user.data);
  }

StatusCode 200 的预期结果响应

StatusCode 400 的实际结果响应

【问题讨论】:

    标签: ionic4 ionic-native cordova-plugin-advanced-http


    【解决方案1】:

    我也有同样的问题;在我的情况下,我通过 将数据序列化程序设置为 'utf8' 并将 Content-Type 标头设置为 'application/x-www-form-urlencoded'

    this.http.setDataSerializer('utf8');
    const httpHeader = {
      'Content-Type':  'application/application/x-www-form-urlencoded'
    };
    

    【讨论】:

    • 真的要写application两次吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    相关资源
    最近更新 更多