$http return status 0 only on device


#1

Hello,

I’ve got an issue whit $http, when i call an http GET to a link , it doesn’t work, but only on device. In emulator and in brower ( ionic serve ), it’s work. with a search, i found that it’s a CORS problem, but i allow CORS in my back end. I don’t know where is the problem…

Thank your for your answer


#2

you need to add cordova-whitelist plugin an allow the connection to your backend since cordova cli 5.0.0


#3

I am having the same issue, but when I add the plugin I get

Plugin “cordova-plugin-whitelist” already installed on android.
Plugin “cordova-plugin-whitelist” already installed on ios.

I also added this to my config.xml

access origin="" subdomains=“true”/
allow-navigation href="
"/

and my response headers on the back end look like this:

res.writeHead(200, {“Access-Control-Allow-Origin”: “*”, “Access-Control-Allow-Headers”: “Content-Type, Authorization, Content-Length, X-Requested-With”});

Do you have any further advice on how to resolve this? Thanks a bunch!


#4

I found the answer to my problem after digging around, and I’ll post the answer in case somebody else has the same problem. My node server was listening to the local ip only, so I had to add a second argument of ‘0.0.0.0’ to the http.createServer() function, like this:

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World\n’);
}).listen(8080, “0.0.0.0”);

This allows requests from other local ips. Works like a charm!