Created
June 16, 2011 02:20
-
-
Save craveytrain/1028553 to your computer and use it in GitHub Desktop.
Cross domain scripting with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajaxPrefilter('json', function(options, orig, jqXHR) { | |
if (options.crossDomain && !$.support.cors) return 'jsonp' | |
}); | |
$.ajax({ | |
url: 'http://nodeserver:3000', | |
dataType: 'json', | |
success: callback, | |
xhrFields: { | |
withCredentials: true | |
} | |
}); | |
function callback(d) { | |
console.log(d); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajaxPrefilter('json', function(options, orig, jqXHR) { | |
if (options.crossDomain && !$.support.cors) return 'jsonp' | |
}); | |
$.ajax({ | |
url: 'http://nodeserver:3000', | |
dataType: 'json', | |
success: callback | |
}); | |
function callback(d) { | |
console.log(d); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
url = require('url'); | |
var json = JSON.stringify([ | |
{ | |
command: 'holla back, yungun', | |
response: 'hooo hooo!' | |
} | |
]); | |
http.createServer(function(req, res) { | |
var reqUrl = url.parse(req.url, true), | |
resp = json, | |
callback = reqUrl.query.callback; | |
if (callback) { | |
resp = callback + '(' + json + ')'; | |
res.writeHead(200, { | |
'Content-Type': 'application/javascript' | |
}); | |
} else { | |
res.writeHead(200, { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*' | |
}); | |
} | |
res.end(resp, encoding='utf8'); | |
}).listen(3000); | |
console.log('Listening on port 3000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment