Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Created July 17, 2012 20:48
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save proudlygeek/3131951 to your computer and use it in GitHub Desktop.
Save proudlygeek/3131951 to your computer and use it in GitHub Desktop.
JSONP Vs. CORS
// http://jsfiddle.net/suBPQ/
$.ajax({
url: "http://api_test_server.proudlygeek.c9.io/",
success: function(data) {
console.log(data);
}
});​
// http://jsfiddle.net/sWaSP/​
$.ajax({
url: "http://api_test_server.proudlygeek.c9.io/?callback=?",
dataType: "jsonp",
success: function(data) {
console.log(data);
}
});​​​​​​​​​​​​​​​​​​
// http://api_test_server.proudlygeek.c9.io/
var http = require("http"),
url = require("url");
var server = http.createServer(function(req, res) {
console.log(req.headers);
var data = {
'name': "Gianpiero",
'last': "Fiorelli",
'age': 37
};
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.end(JSON.stringify(data));
});
server.listen(process.env.PORT, process.env.IP);
console.log('Server running at ' + process.env.PORT + ':' + process.env.IP);
// http://api_test_server.proudlygeek.c9.io/?callback=cb
var http = require("http"),
url = require("url");
var server = http.createServer(function(req, res) {
var callback = url.parse(req.url, true).query.callback || "myCallback";
console.log(url.parse(req.url, true).query.callback);
var data = {
'name': "Gianpiero",
'last': "Fiorelli",
'age': 37
};
data = callback + '(' + JSON.stringify(data) + ');';
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(data);
});
server.listen(process.env.PORT, process.env.IP);
console.log('Server running at ' + process.env.PORT + ':' + process.env.IP);
@Antonio-perez-alvarez
Copy link

Fix res.writeHead(200, {'Content-Type': 'application/json'}); to res.writeHead(200, {'Content-Type': 'application/javascript''});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment