【问题标题】:Ionic native Http post not working in IOS离子原生 Http 帖子在 IOS 中不起作用
【发布时间】:2021-03-25 16:48:44
【问题描述】:

我需要帮助来解决这个问题。我在执行 POST 请求以获取 IOS 中的访问令牌时遇到问题。而它在 Android 中运行良好。

我正在使用 Cordova 插件cordova-plugin-advanced-http: "^2.4.1"

代码:

const bodyParams = {‘client_id’:clientId,‘client_secret’:secret,‘grant_type’:‘authorization_code’,‘code’:tokenOrCode};
const httpResponse = await this.http.post(accessTokenUrl, bodyParams, {“Content-Type”: “application/json”});

错误:

{“error”:“invalid_client”,“error_description”:“FBTOAU204E An invalid secret was provided for the client with identifier: ‘abc@abc.com’.”}

注意:以上代码在 Android 中运行没有任何问题。

Package.json:

"@angular/common": "^7.2.2",
"@angular/core": "^7.2.2",
"@angular/forms": "^7.2.2",
"@angular/http": "^7.2.2",
"@angular/platform-browser": "^7.2.2",
"@angular/platform-browser-dynamic": "^7.2.2",
"@angular/router": "^7.2.2",
"@ionic-native/core": "^5.0.0",
"@ionic-native/http": "^5.2.0",
"@ionic-native/network": "^5.24.0",
"@ionic-native/secure-storage": "^5.2.0",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^4.1.0",
"ajv": "^6.10.0",
"cordova-ios": "^4.5.5",
"cordova-plugin-advanced-http": "^2.0.9",
"cordova-plugin-cookiemaster": "^1.0.5",
"cordova-plugin-device": "^2.0.3",
"cordova-plugin-file": "^6.0.2",
"cordova-plugin-ionic-keyboard": "^2.2.0",
"cordova-plugin-ionic-webview": "^3.0.0",
"cordova-plugin-network-information": "^2.0.2",
"cordova-plugin-secure-storage": "^3.0.2",
"cordova-plugin-splashscreen": "^5.0.3",
"cordova-plugin-statusbar": "^2.4.3",
"cordova-plugin-whitelist": "^1.3.4",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"zone.js": "~0.8.29"

【问题讨论】:

    标签: ionic-framework wkwebview ionic5 cordova-plugin-advanced-http ionic-native-http


    【解决方案1】:

    在根据您的请求发送发布请求之前设置序列化器:

    对于json:

    this.http.setDataSerializer("json");
    const bodyParams = {‘client_id’:clientId,‘client_secret’:secret,‘grant_type’:‘authorization_code’,‘code’:tokenOrCode};
    const httpResponse = await this.http.post(accessTokenUrl, bodyParams, {“Content-Type”: “application/json”});
    

    【讨论】:

    • 我尝试使用 DataSerializer,但仍然是同样的问题。
    • 您找到解决问题的方法了吗? @RAJESKUMAR
    • 这个解决方案我已经在 ionic 3 中实现了。
    • 它对我不起作用,但我通过更改我的内容类型解决了这个问题 - {“Content-Type”:“application/x-www-form-urlencoded”}
    • 我认为这可以在服务器上处理
    【解决方案2】:

    我遇到了与您的问题类似的问题,例如 request 在 Android 上运行,在 Ios 上不运行。 (Ionic 5 - 电容器应用)

    我解决了更改内容类型的问题。

    也许对你也有帮助,请尝试一下。

    {“Content-Type”: “application/json”}
    

    替换为以下内容

    {“Content-Type”: “application/x-www-form-urlencoded”}
    

    【讨论】:

      最近更新 更多