My code looks like this, I want to practice with jQuery's ajax function ( I have already run npm install jquery
to install the package):
var $ = require('jquery');
var remoteValue = false;
var doSomethingWithRemoteValue = function() {
console.log(remoteValue);
}
var promise = $.ajax({
url: 'https://google.com'
});
//outputs "true"
promise.always(function() {
remoteValue = true;
doSomethingWithRemoteValue();
});
//outputs "foobar"
promise.always(function() {
remoteValue = 'foobar';
doSomethingWithRemoteValue();
});
But the NodeJS compiler complains that there is no ajax
method in jQuery module.
bash-3.2$ node test.js
/Users/hanfeisun/Downloads/node/test.js:14
var promise = $.ajax({
^
TypeError: Object function ( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
} has no method 'ajax'
at Object.<anonymous> (/Users/hanfeisun/Downloads/node/test.js:14:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Shouldn't the javascript codes of jQuery for Browser JS Compiler and NodeJS the same?
If not, does anyone have idea about the main reason? Thanks!