$ .ajax不是函数吗?

I have recently started to learn Javascript and I am working on a small project. I am having an error stating "$.ajax is not a function". endpoint, token, and body are defined variables.

var $ = require('jQuery');
$.ajax({
    type: 'POST',
    url: endpoint + token,
    contentType: 'application/json',
    data: JSON.stringify(body),
    success: function(data, status) {
        console.log(data);
        console.log(status);
    },
    error: function(jqXHR, status, errorThrown) {
        console.log(errorThrown);
        console.log(status);
        console.log(jqXHR);
    }
});

Anyone see a problem with what I have?

2 Possible Reasons :

  1. Check if jQuery has been added in your index.html (or whatever name you main html has) file.
  2. If above does not relate to your problem, then you need to put a check i.e. is jQuery is available to you, something like below code :

    if(window.jQuery)
        { 
          ...do your stuff here
         }
          else{
                ... handle it according to your need here
               }
    
  3. Also, don't forget to check if your document object is ready, if you are going to fetch a value from html inputs.

     $( document ).ready(function() {
        console.log( "ready!" );
     });