Ionic 3 Response with Status: 0 for Url: Null

Ionic 3 Response with status: 0 for URL: null

To avoid CORS problem specially in iOS you must use @ionic-native/http plugin which is actually Advanced HTTP plugin for API calling.

Follow below steps to use this plugin

Step 1: Add Http native plugin

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

Installation Link : HTTP

Step 2: Import HTTP native plugin in your file where you wants to cal API.

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

Step 3: How to use this plugin for API call ?

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

Hope this will help you.

Response with status: 0 for URL: null – getting this error when I run in simulator or web

This seems like a CORS issue, To avoid CORS problem you must use @ionic-native/HTTP plugin which is actually Advanced HTTP plugin for API calls.

Follow below steps to use this plugin

Step 1: Add Http native plugin

$ ionic cordova plugin add cordova-plugin-advanced-http
$ npm install --save @ionic-native/http
Installation Link : HTTP

Step 2: Import HTTP native plugin in your file where you wants to call API.

import { HTTP, HTTPResponse } from '@ionic-native/http';
Step 3: How to use this plugin for API call ?

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

Respon with status: 0 for URL: null

Finally… i solve this problem with change from http to https and check header send. Headher from jwt is my problem, so i change it using post not header.

Error: Uncaught (in promise): Response with status: 0 for URL: null status: 404

here is the signature of http post method in angular

post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;

You can try the following way

var options = new RequestOptions();
options.headers = new Headers();
options.headers.append( 'Content-Type', 'application/x-www-form-urlencoded');
let curr_page = { "currentPage":1,"pageSize":2 };
// var page_size = 5;
this.http.post(this.db,JSON.stringify(curr_page),options).map(res=>res.json())
.subscribe(res=>{
console.log(res);
resolve(res);
}, (err) => {
reject(err);
});

Ionic 4 Http failure response for (unknown url): 0 Unknown Error. when calling api on real device

Finally, I could resolve problem after two days hardwork !
I migrated API calling from '@angular/common/http' to native http '@ionic-native/http/ngx'
with this header:

        // create headers data
const headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
'Accept': 'application/json, text/plain',
"cache-control": "no-cache",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token, Accept, Authorization, X-Request-With, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Credentials" : "true",
"Access-Control-Allow-Methods" : "GET, POST, DELETE, PUT, OPTIONS, TRACE, PATCH, CONNECT",
};

The cons for me, For now on I have to test on a real device.



Related Topics



Leave a reply



Submit