Ionic 3响应状态:0表示URL:null

我有一个Ionic 3应用程序。 我最近添加了iOS平台。

当我在iOS(模拟器和设备)上运行它时,所有具有标头的服务器请求都会失败并显示错误“响应状态:0表示URL:null” 。 在Android上,这些请求工作正常。

如果我执行没有标头的请求我会从服务器获得预期的响应。

我知道问题出在WKWebViewCORS上 。 服务器正确配置了CORS。 我用@ angular / http模块做请求。

我们来看一些代码。

这是我向服务器发出请求的提供者:

import { Injectable } from '@angular/core'; import { Content } from 'ionic-angular'; import { Http, Headers, RequestOptions, URLSearchParams } from '@angular/http'; import { HTTP } from '@ionic-native/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import { Globalization } from '@ionic-native/globalization'; import { Globals } from '../providers/globals'; ... /// Here we have one example Request (it's inside a method) /// Here I create the URL for the request let url = this.server_url + this.server_functions.doSearch; /// Now I create the Headers for the Request let headers = new Headers(); /// As You can see, I have tried to pass diferent headers to server // headers.append("Access-Control-Allow-Origin","*"); // headers.append("Origin", "https://localhost:8080"); // headers.append("Access-Control-Allow-Methods","POST, GET, OPTIONS, PUT"); // headers.append("Accept","application/json"); // headers.append("Content-Type","application/json; charset=utf-8"); /// If the user is logged in I have to send this headers to server if ( !this.globals.guestMode ) { headers.append("TokenAuth", this.globals.getLogRegData()["TokenAuth"]); headers.append("IdAuth", this.globals.getLogRegData()["IdAuth"]); } /// And here we start the GET Request this.http.get( url, { headers: headers } ).map(res => res.json()).subscribe( data => { // console.log( JSON.stringify(data) ); callback( data ); }, err => { console.log("ELOL: "+err); } ); 

换句话说,我决定尝试@ ionic-native / http模块(你可以在导入中看到)以避免WKWebView和CORS问题,但是当我用它做请求时,我收到了这个错误:

 WARN: Native: tried calling HTTP.get, but the HTTP plugin is not installed. WARN: Install the HTTP plugin: 'ionic cordova plugin add cordova-plugin-advanced-http' 

这是我使用本机插件执行请求的方式:

 this.httpnative.get(url, {}, {}) .then(data => { console.log(data.status); console.log(data.data); // data received by server console.log(data.headers); }) .catch(error => { console.log(error.status); console.log(error.error); // error message as string console.log(error.headers); }); 

这是我app.module.ts的一个片段:

 import { HttpModule } from '@angular/http'; import { HTTP } from '@ionic-native/http'; ... @NgModule({ ... imports: [ ... HttpModule, HTTP, ... ], }) 

我希望有人可以为我带来一些启示,因为我迷失在离子的道路上。

谢谢。

为了避免在iOS特别出现CORS问题,您必须使用@ ionic-native / http插件,它实际上是用于API调用的高级HTTP插件。

请按照以下步骤使用此插件

第1步:添加Http本机插件

 $ ionic cordova plugin add cordova-plugin-advanced-http $ npm install --save @ionic-native/http 

安装链接: HTTP

第2步:在您要调用API的文件中导入HTTP本机插件。

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

第3步:如何使用此插件进行API调用?

 constructor(public httpPlugin: HTTP) { } //Set header like this this.httpPlugin.setHeader("content-type", "application/json"); //Call API this.httpPlugin.get(this.url, {}, {}).then((response) => { //Got your server response }).catch(error => { //Got error }); 

希望这会帮助你。

我在android中有相同的问题,rest api在android模拟器或设备中不起作用并且给出错误:Url的响应为0:null。 最后我得到解决方案,有cordova平台Android版本问题,如果有Cordova Android版本是6.3然后它的工作正常但它有Cordova Android版本是7.0或7.1.1然后它不适合我。

所以,我认为在ios中你应该检查你的ios平台版本,然后再试一次。 你可以使用cli命令检查ios或android版本。 离子信息

还要检查是否安装了所有的cordova插件。 如果有人丢失,请手动安装并再次检查。 您可以使用cli命令检查您的cordova插件。 cordova插件-ls

我认为这对你很有帮助。

谢谢