2021年4月11日星期日

$.get is not a function, Synchronous XMLHttpRequest on the main thread is deprecated

I am writing the JavaScript for a webpage that I'm building. I am using the following (simplified) code successfully:

$('#selector').on('click', function () {      if (isTrue) {      if (isTrue) {      }      else {        // execute block of code 'A', which is similar to 'B'        callAjax(url, function() {            // do some things...        })      }    }    else {      // execute block of code 'B', which is similar to 'A'      callAjax(url, function() {          // do some things...      })    }   });    /**************************  * Call Ajax  **************************/  function callAjax(url, callback) {      $.get(      url,       function(data) {            callback();      }    )  }  

However, I want to improve the code by modularizing it like so:

$('#selector').on('click', function () {    if (isTrue) {      if (isTrue) {      }      else {        handler(objects);      }    }    else {      handler(objects);    }  });        /**************************  * Handler  **************************/  function handler(objects) {      callAjax(url, function() {        // do some things...    })  }    /**************************  * Call Ajax  **************************/  function callAjax(url, callback) {      $.get(      url,       function(data) {            callback();      }    )  }  

Whenever I run the second set of code, it throws the "Synchronous XMLHttpRequest on the main thread is deprecated" warning. Subsequent runs through the code throw the "$.get is not a function" error, and the code stops running as soon as handler() is called.

I am not referencing the slim version of JavaScript.

Why are these errors and warnings being thrown, and how can I successfully simplify my code in the way I have described?

EDIT:

To add clarity and to answer the comments I have so far, here are the scripts I've included at the bottom of the webpage within the <body></body> tags:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>  
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>  
https://stackoverflow.com/questions/67050233/get-is-not-a-function-synchronous-xmlhttprequest-on-the-main-thread-is-deprec April 12, 2021 at 05:13AM

没有评论:

发表评论