Ionic 3 Resposta com status: 0 para URL: null
Eu tenho um aplicativo Ionic 3. Eu adicionei recentemente a plataforma iOS.
Quando o executo no iOS (emulador e dispositivo), todas as solicitações de servidor com cabeçalhos falham com o erro"Resposta com status: 0 para URL: null". No Android, esses pedidos funcionam bem.
Se eu fizer os pedidossem cabeçalhos Eu recebo a resposta esperada do servidor.
Eu sei que o problema é comWKWebView eCORS. O servidor possui o CORS configurado corretamente. Eu faço os pedidos com@ angular / http módulo.
Vamos ver algum código.
Este é o meu provedor para fazer solicitações ao servidor:
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);
}
);
Por outro lado, decidi tentar o módulo @ ionic-native / http (como você pode ver nas importações) para evitar os problemas do WKWebView e do CORS, mas quando faço a solicitação, recebi o seguinte erro:
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'
É assim que eu faço a solicitação com o plug-in nativo:
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);
});
Este é um fragmento do meu app.module.ts:
import { HttpModule } from '@angular/http';
import { HTTP } from '@ionic-native/http';
...
@NgModule({
...
imports: [
...
HttpModule,
HTTP,
...
],
})
Espero que alguém possa me trazer alguma luz sobre isso, porque estou muito perdido nos caminhos da Ionic.
Obrigado.