Skip to content

Instantly share code, notes, and snippets.

@craveytrain
Created June 16, 2011 02:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craveytrain/1028553 to your computer and use it in GitHub Desktop.
Save craveytrain/1028553 to your computer and use it in GitHub Desktop.
Cross domain scripting with jQuery
$.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);
};
$.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);
};
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