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

时间:2019-09-04 12:13:01

标签: ios angular api ionic4

我有一个带有Angular的Ionic 4应用程序,可以连接到我的API。 在Android /浏览器上,对API的调用正在运行(控制台上没有错误)。但是,在iOS上,有时会出现错误“ status”:0,“ statusText”: “未知错误”

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}}

奇怪的是,这并没有一直发生。

场景:

  1. https://api.domain.com/v2/profile的有角httpClient GET调用=>可以正常工作,将数据取回
  2. 另一个https://api.domain.com/v2/list的GET调用(进行了导航)=>有效
  3. 返回上一页,对https://api.domain.com/v2/profile的新GET调用失败,并引发错误。

导航多少次无关紧要,它会随机引发上述错误。如果与CORS有任何关系,我认为每次您在iOS上进行请求时,它都会引发错误。

离子信息

离子:


   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控制台屏幕截图:https://imgur.com/a/BNXAB7Y

1 个答案:

答案 0 :(得分:0)

此答案使我可以找到答案Preflight response is not successful with proper headers

离子的常见问题解答有助于理解https://ionicframework.com/docs/faq/cors

对我来说,此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]