简体   繁体   English

Ionic 4 iOS 应用程序对 API 的请求引发未知错误,状态 0

[英]Ionic 4 iOS app request to API throws error unknown, status 0

I have a Ionic 4 with Angular app which connects to my API.我有一个带有 Angular 应用程序的 Ionic 4,它连接到我的 API。 On Android / Browser the calls to the API are working (no errors on the console).在 Android / 浏览器上,对 API 的调用正在运行(控制台上没有错误)。 However on iOS I sometimes get an error "status":0,"statusText": "Unknown Error"但是在 iOS 上我有时会收到错误 "status":0,"statusText": "Unknown Error"

ERROR: {
"headers":{"normalizedNames":{},
"lazyUpdate":null,
"headers":{}},
"status":0,
"statusText": "Unknown Error",
"url":"https://api.domain.com/v2/profile",
"ok":false,
"name":"HttpErrorResponse",
"message":"Http failure response for https://api.domain.com/v2/profile: 0 Unknown Error",
"error":{"isTrusted":true}}

the weird thing is, this is not happening all the time.奇怪的是,这并非一直发生。

scenario:设想:

  1. Angular httpClient GET call to https://api.domain.com/v2/profile => Works, getting data back Angular httpClient GET 调用https://api.domain.com/v2/profile => 工作,取回数据
  2. Another GET call (navigation occured) to https://api.domain.com/v2/list => Works另一个 GET 调用(发生导航)到https://api.domain.com/v2/list => Works
  3. Going back to previous page a new GET call to https://api.domain.com/v2/profile fails and throws above error.返回上一页,对https://api.domain.com/v2/profile的新 GET 调用失败并引发上述错误。

It doesn't matter how many times you navigate, it randomly throws the above error.无论您导航多少次,它都会随机抛出上述错误。 If it had anything to do with CORS I assume it would throw the error everytime you do a request on iOS.如果它与 CORS 有任何关系,我认为它会在您每次在 iOS 上执行请求时抛出错误。

Ionic info离子信息

Ionic:离子:


   Ionic CLI                     : 5.2.7 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.8.1
   @angular-devkit/build-angular : 0.801.3
   @angular-devkit/schematics    : 8.3.2
   @angular/cli                  : 8.3.2
   @ionic/angular-toolkit        : 2.0.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : ios 5.0.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 10 other plugins)

Utility:

   cordova-res : 0.6.0 
   native-run  : 0.2.8 

System:

   ios-sim : 8.0.2
   NodeJS  : v10.16.3 (/usr/local/bin/node)
   npm     : 6.9.0
   OS      : macOS Mojave
   Xcode   : Xcode 10.2.1 Build version 10E1001

Safari console screenshot: https://imgur.com/a/BNXAB7Y Safari 控制台截图: https : //imgur.com/a/BNXAB7Y

This answer allows me to find the answer Preflight response is not successful with proper headers这个答案让我可以找到答案Preflight response is not success with正确的标题

The ionic's FAQ helps to understand https://ionicframework.com/docs/faq/cors . ionic 的 FAQ 有助于理解https://ionicframework.com/docs/faq/cors

For me, this Apache's configuration works done in virtual host conf:对我来说,这个 Apache 的配置是在虚拟主机 conf 中完成的:

# Always set these headers.
#you can restrict the domain to allow here
SetEnvIf Origin "^(.*)$" AccessControlAllowOrigin=$0
Header always set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM