0

Here is the code, I wanted to create js code to request a web service where there is form data and printing these data in console I run this code with Node.js:

//importing jQuery
var  $ = require('jQuery');

//defining the parameters of HTTP request
var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://www.kizeoforms.com/rest/v3/forms/",
  "method": "GET",
  "headers": {
    "content-type": "application/json",
    "Authorization": "perma_restv3_MTH_024b61f4bf72fd4c46d7633be7e21e083d8b660f",
    "cache-control": "no-cache",
  }
}

//sending request and printing of response in console
$.ajax(settings).done(function (response) {
  console.log(response);
});

Here is what the console send me :

   $.ajax(settings).done(function (response) {
  ^

TypeError: $.ajax is not a function
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
[Finished in 0.1s]

I have already installed jQuery now I'm pretty much stuck with this problem,

Thank you,

JS1
  • 631
  • 2
  • 7
  • 23

1 Answers1

1

jQuery was designed to run in a web browser, not in Node.js. It really isn't a good fit for Node.js.

See the documentation which says:

For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as jsdom. This can be useful for testing purposes.

It then gives an example of how to do this… but the example does not work because the features of jsdom that it uses are deprecated.

If you want to perform Ajax server-side, you would be better off using a library designed to work on Node.js. Axios is a popular choice. If you want to fetch HTML with it and then process it with a jQuery-like API then look to Cheerio.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335