Created
July 9, 2015 11:52
-
-
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)
This file contains hidden or 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 $ = 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); | |
} | |
}); |
This file contains hidden or 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
{ | |
"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