0

How to make HTTP Get request from the ajax part of the JS code?

$.ajax({
    type: "POST",
    url: "<?= base_url() ?>captcha/checkcaptcha",
    global: false,
    data: {captcha: captcha},
    dataType: 'JSON',
    async: false,
    success: function (data) {
        if (data.error)
        {
            // GET REQUEST FROM HERE
            const http = new XMLHttpRequest();
            http.open("GET", "<?= base_url() ?>captcha/error");
            http.send();
        } else if (data.success)
        {
            status = true;
        }
    }
});
return status;

I get in a browser console:

jquery.min.js:4 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

thinker
  • 402
  • 1
  • 6
  • 15
  • 1
    Like it says, synchronous requests are deprecated. Asynchronous requests are nearly always preferable, for reasons you can look up. Just remove the param `async: false` – Mitya Sep 15 '18 at 10:40
  • @Utkanos for some reason when I set it to TRUE, then validation doesn't work at all, but without errors in browser console. – thinker Sep 15 '18 at 12:05
  • Well you don't show us your validation code so I can't comment there. Often when moving from sync to async you need to rework some of your dependent code to utilise callbacks or promises, otherwise, unlike with sync, code can run in an order opposite to that which you intend. That's the most common pitfall when moving to async. – Mitya Sep 16 '18 at 09:49

0 Answers0