Hi, everyone.
In my current project for google chrome webstore I am using nodejs as server for saving high scores of the game. And in the client side - jQuery for receiving high scores. But I found one problem connecting with jsonp format.
When I tried to send request to the nodejs url(http://oivoodoo.no.de/json) for getting the json data with scores I saw issue(in chrome) connecting with cross domain requests. My nodejs server was returning data in jsonp format(Content Type - application/javascript).
At first time I write the next following code for jquery:
$.getJSON('http://oivoodoo.no.de/json', function(data) {
var html = "";
$.each(data, function(i, o) {
html += "<li>" + o.username + ": " + o.scores + "</li>";
});
$("#scores_table").html(html);
});
app.get('/:format?', function(req, res) {
Score.find({}, function(err, scores) {
switch(req.params.format) {
case 'json':
res.header("Access-Control-Allow-Origin", "*");
res.send(scores.map(function(s){
return s.toObject();
}));
break;
default:
res.render('index.jade', {
locals: { scores: scores }
});
}
});
});
As you can see I’ve added ‘res.header(“Access-Control-Allow-Origin”, “*”);’ this line to the json request. And it works!!!