Ionic View - Response with status: 0 for URL: null

Hi guys!

Im facing this error and had looked for all topics and dont got able to solve it.
My app is working well on browser.
I had set properly the origins both in app and in server (my restful service is an Aspnet).
Also I tried with http and https where I had the same result.

So there are things that I tried:

1 - set the allow origin in app and server;
2 - tried with HTTP and HTTPS (both worked with postman);
3 - delete the app on ionicview and upload again changing the appid;
4 - tested on iOS and Android (with the IonicView);
5 - I have the CORS enable with config.EnableCors(); in aspnet service. [EDIT]

[EDIT]
This is a print of a success called by browser with ionic serve.
image

Any tips?
Thanks!

So you are testing your app in Ionic View (Android and iOS) and your URL request, that worked in browser, doesn’t work any more? Correct?

If so:
Did you remote debug the app? https://docs.ionic.io/tools/developer/#remote-debugging
Did you put a proxy in between Ionic View (your device) and the server to see if the request actually goes out?

Hey Sujan12!
Thanks for your time.

I made some changes on my app and its working now.

let token:string = "Bearer " + this.getCurrentToken();

let header = new Headers();
header.append(‘Authorization’, token);
header.append(‘Access-Control-Allow-Origin’,’*’);
header.append(‘Content-Type’, ‘application/x-www-form-urlencoded’);
header.append(‘Cache-Control’, ‘no-cache’);

I think that what make it works was the Content-Type header.

In the service, I was using:

config.EnableCors();

And changed it for this method:

private static void EnableCrossSiteRequests(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute(
            origins: "*",
            headers: "*",
            methods: "*");
        config.EnableCors(cors);
    }

Thanks again!

Hi,

Another detail that can be helpful is that when you need to do a call with info in the body (like the postman get) its not needed to pass a header. In this example, Im getting a token from my server.

return this.http.post(‘url/token’,
‘grant_type=password&username=’+ user +’&password=’ + pass, reqOptions)
.toPromise()
.then(response => {
const access_token = response.json().access_token;
localStorage.setItem(‘access_token’, access_token);
return access_token;
})
.catch(onerror => {
console.warn(onerror);
throw onerror;
});