Working on a RESTful api, When I access this in my browser at 96.126.111.211/getLeaderBoard
it works fine and gives me the JSON data from my mongo database.
Though when I try a jquery ajax request like this http://jsfiddle.net/yVQ5T/ I get a jsonp error
Error: jQuery171037622810900211334_1363808084624 was not called
Here is the code on the server side
getLeaderBoard(req, res)
{
this.db.collection(this.settings.userTable, function(err, collection) => {
collection.find().toArray(function(err, items) {
res.writeHead(200, {'Content-Type': 'application/javascript'});
res.write(JSON.stringify(items));
res.end();
});
});
}
res.json(items)or more specifically,res.jsonp(items)which should solve your problem. There is no need to write the data manually to the response (or end) from Express unless you have special circumstances. Also, good practice in Node.js is to explicitly return the final statement in each function to guarantee you leave it when you expect to leave it.