1

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>

I suppose I should include this as well. It references the .js file I'm writing.

<script src="script.js"></script>
  • Are you using jQuery? Is it included? – Markus Zeller Apr 11 '21 at 21:14
  • 1
    Might be helpful to look at https://stackoverflow.com/questions/24639335/javascript-console-log-causes-error-synchronous-xmlhttprequest-on-the-main-thr – Twisty Apr 11 '21 at 21:23
  • Sure you aren't loading jQuery.js twice and the second one is a slim version? – charlietfl Apr 11 '21 at 21:40
  • Thank you for commenting. I've edited my post so you can see what scripts I have included in my webpage. Thank you for referencing that post @Twisty. I've looked at it quite a few times but nothing has stuck out to me. I may go over it again and try some things I hadn't before. – garyshackle Apr 12 '21 at 04:11
  • *Danger*: You are using a very old version of jQuery. Upgrade to the latest versions. Get the security fixes. – Quentin Apr 13 '21 at 13:52

1 Answers1

0

After examining my code and referring to the post at the following link:

JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..."

I realized that I had a bug in my code. I was passing an "undefined" url to the ajax request, and not a string path as I was expecting.