Skip to content

Instantly share code, notes, and snippets.

@mugifly
Created July 9, 2015 11:52
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 mugifly/c847f9e7b535f24b2a75 to your computer and use it in GitHub Desktop.
Save mugifly/c847f9e7b535f24b2a75 to your computer and use it in GitHub Desktop.
Ajax with using jQuery on Node.js (Tested on Node.js v0.12.5)
var $ = require('jquery')(require('jsdom-no-contextify').jsdom().parentWindow);
// Support for Cross-domain request with using jQuery
// See: http://garajeando.blogspot.jp/2012/06/avoiding-xmlhttprequest-problem-using.html
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
$.support.cors = true;
$.ajaxSettings.xhr = function () {
return new XMLHttpRequest;
}
$.ajax({
url: 'http://example.com/',
type: 'GET',
success: function(data, status, jq_xhr) {
console.log(data);
},
error: function(jq_xhr, status, error_str) {
console.log(error_str);
}
});
{
"name": "jquery-ajax-with-node.js",
"version": "1.0.0",
"main": "jquery-ajax-with-node.js",
"scripts": {
"start": "node jquery-ajax-with-node.js"
},
"dependencies": {
"jquery": "^2.1.4",
"jsdom-no-contextify": "^3.1.0",
"xmlhttprequest": "^1.7.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment