I have a node js server running express js and created a front-end client using the ionic-framework (cordova/phonegap). I am able to successfully make ajax calls to my server and receive proper replies when running the client on the same computer the server is being hosted on because they are running on the same IP.
The problem comes in when I try to run the client on a different IP from the server. I have read into setting up cors on the server side. I have it set up pretty much the same as how it is set up here: Allow CORS REST request to a Express/Node.js application on Heroku
Here is my cors setup:
var express = require('express');
var compress = require('compression');
var bodyParser = require('body-parser');
var app = express();
var cors = function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
appLogger.info('testing');
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(cors);
app.use(compress());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function(err, req, res, next) { // Handle errors...
res.send(500, err.toString());
appLogger.error(err.stack);
});
When running the ui on the host computer, I see 'testing' in the console so I know it is making the first pass through there.
I also checked the white listing on my ui application in the config.xml:
<access origin="*" subdomains="true"/>
I have tried the following ajax query:
$.ajax({
type: "POST",
url: "http://hostIPAddress:7777/login",
crossDomain: true,
async: true,
data: {
email: $scope.accountData.email,
password: $scope.accountData.password
},
dataType: "json",
timeout: 5000
})
.done(function(data, textStatus, jqXHR) {
var json = data;
alert(JSON.stringify(json));
console.log(JSON.stringify(json));
})
.fail(function(jqXHR, textStatus, errorThrown) {
alert('Error logging in: ' + errorThrown);
alert(JSON.stringify(jqXHR));
});
I have also tried using Angular JS's $http
$http({
method : 'post',
url: 'http://hostIPAddress:7777/login',
data: data,
timeout: 5000
})
.success(function(data, status, headers, config) {
alert('Success');
})
.error(function(data, status, headers, config) {
alert('Fail: ' + JSON.stringify(data));
alert('Status: ' + JSON.stringify(status));
alert('Headers: ' + JSON.stringify(headers));
alert('Config: ' + JSON.stringify(config));
});
Again, this all (both the regular ajax call and angular js's $http method) works when the client is running on the same computer as the node server. I have also tried the ajax call with / without crossDomain: true to no avail.
Other info: When I run the ajax post call, I get nothing relevant back when the call fails. Everything is null or blank. The $http call returns with a 404 status like it can't find the host. I am able to ping the IP associated with the host when the server is not running but when I ping the IP when the server is running, the ping back times out.
Anybody have any thoughts on what may be happening? Please let me know if I can provide any more helpful information.
Thanks in advance!
update - 11/23/2014 11:08pm
Here is my header that's being sent by my client ui:
Request URL:http://myserverip:7777/login
Request Headers
Provisional headers are shown
Accept:application/json, text/javascript, */*; q=0.01
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:http://localhost:8100
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36
Form Data
email:testemail@gmail.com
password:password1
update - 11/24/2014 11:27pm
I was able to successfully run a http request via curl from my phone to the server. So I know my server is being responsive to external requests. It is the ionic client that is having an issue.
update - 11/26/2014 9:18am
I tried navigating to the url from the browser of one computer to another and it times out. I've tried, chrome, safari, and IE. I tried curl from two different computers and it cannot find the host. I am able to navigate to the url from my iPhone and it returns a proper response but if I do the same thing from my android phone, it cannot find the host. So for whatever reason, I can only find the host with my iPhone.