Reputation: 317
i have an ionic app and i'm trying to test it on my android device. the app works when testing it on the browser using 'ionic serve' but when i run 'ionic run android' the post request does not seem to hit the server. I'm getting an error
'POST http://localhost:3000/login net::ERR_CONNECTION_REFUSED'
for the server i'm using nodejs and have access-control..
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header(
'Access-Control-Allow-Headers',
'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
);
if ('OPTIONS' === req.method) {
res.status(200).end();
} else {
next();
}
});
i've also installed cordova whitelist plugin and the meta content-security-policy like so..
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline'; media-src *; img-src * data:;"/>
but i'm still getting the same error. i've also tried using my ip instead of localhost and got this error.
OPTIONS http://192.168.1.66:3000/login net::ERR_CONNECTION_TIMED_OUT
i've followed this thread.. net::ERR_CONNECTION_REFUSED ionic suggesting that i enable CORS by installing the microsoft.aspnet.webapi.cors.. i followed this link https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors and now i'm totally lost. please help, i've been on this for 3 days now. i appreciate all your help. thank you.
Upvotes: 1
Views: 2004
Reputation: 28599
You are getting a CONNECTION_REFUSED when you try to connect to localhost
from the device, which makes sense - as the server process is not running on the device - it is running on your machine.
The second example is more correct, in that from the device, you are trying to connect to your machine - but we see a different error: CONNECTION TIMEOUT
This one is actually due to your local firewall; you will by default allow connections to that port from within your local host - but we need to do some magic to allow packets from the world to talk to that service.
Are you running the server process on *nix? Or Windows?
For *nix
# Using firewalld (advanced wrapper around iptables)
firewall-cmd --zone=public --add-port=tcp/3000 --permanent
firewall-cmd --reload
# Using bare iptables
iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
For Windows
Open the Firewall configuration, and under Inbound Rules find the listing for the server process that you are running.
Double click the entry to open the properties window - in here click Allow the Connection, and Apply.
From the device
Try to again connect to the server process on your computer.
Upvotes: 1