I am migrating my work from an old environment to a newer one. Im in the middle of updating outdated code with newer one and it seems to be running well(my web service written in node.js)
I have my webservice running in node.js and my site using jquery. node.js code
var express = require('express');
var app = express();
app.get('/', function(req, res){
console.log('GET request to /');
});
app.listen(8888);
console.log('Express server started on port 8888');
Here is my jquery code
$.getJSON('http://URL:8888/getTables', function(data){
//stuff
});
however in firebug the console shows:
"http://URL.com:8888/getTables 200 OK 64ms"
with no response
if i go to the site directly "http://URL.com:8888/getTables" i get my data.
any ideas what is going on? i'll leave the web service running so you can see what im talking about.
the site that is using it is http://www.URL.com/db/index.html
edit:
app.get('/getTables', function(req, res){
console.log('GET request to /getTables');
getTables(req, res);
sendJSON(res, 200, cache_tables);
});
function sendJSON(res, httpCode, body)
{
var response = JSON.stringify(body);
res.send(response, {'Access-Control-Allow-Origin': '*'}, httpCode);
}
UPDATE
I found my solution, thanks for letting me know my issue was cross-domain, i thought my sendJSON took care of it but since i upgrading nodejs and the modules it probably become obsolete. Here is the fix for those wondering:
//Middleware: Allows cross-domain requests (CORS)
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
//App config
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret' }));
app.use(express.methodOverride());
app.use(allowCrossDomain);
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
and i had to update all of my gets to use:
app.get('/', function(req, res, next){
console.log('GET request to /');
});