Why Is There No $.ajax Method In Jquery Package For Node.js?
Solution 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.
Solution 2:
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 } = newJSDOM( "" );
const $ = require( "jquery" )( window ); // or const jquery = require( "jquery" )( window );
Solution 3:
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.
Post a Comment for "Why Is There No $.ajax Method In Jquery Package For Node.js?"