2

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!

3 Answers 3

1

I'm not too familiar with Node's javascript environment, but the point of ajax is to handle a server response with javascript without causing a page refresh. On a server running Node, you would just be able to make a normal HTTP request.

In fact, a quick google search shows that the underlying XMLHttpRquest that JQuery builds on with $.ajax doesn't exist in Node (and in fact the Wikipedia article on XHR suggests it's part of the Browser object model), so it wouldn't be available from Node (though you can probably emulate it by leveraging Node's http request module.)

If you want to experiment with $.ajax, you should do it within a browser environment. You could do it from the javascript console from any webpage that includes JQuery, such as Stackoverflow, or make your own test HTML page with a Script tag in the header that sources JQuery from a CDN.

Sign up to request clarification or add additional context in comments.

Comments

0

As the error states you need a window object to work with jquery. You will need JSDOM to use Jquery methods. Try this.

const { JSDOM } = require( "jsdom" ); 
const { window } = new JSDOM( "" ); 
const $ = require( "jquery" )( window ); // or const jquery = require( "jquery" )( window );

Comments

-1

Javascript (server-side) for Node.js is a bit different language from usual client side javascript. It is a javascript but has different context environment.

jQuery supposed to help the client side javascript only. Try the following link for further information.

Client side scripting and Server side scripting languages

1 Comment

It is not a different language. It is JavaScript. It just happens that the DOM APIs and networking APIs most people are familiar with are only present in browsers, and the Node's APIs are only present in Node. jQuery can run anywhere the DOM APIs and XMLHttpRequest are available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.