V
V
Vladislav Kopylov2015-02-17 10:42:36
JavaScript
Vladislav Kopylov, 2015-02-17 10:42:36

"Synchronous XMLHttpRequest on main thread is deprecated" error in some browsers. How to fix?

In google chrome the code runs fine. But in firefox and opera, this code does not work - it gives an error.

function GetAddFormAndOpenModal(form, promo, code){

    if(form){
      $.post('/personal/items/add/', 
       { itemadd : form['itemadd'].value, submitform : form['submitform'].value, checkbutton : form['checkbutton'].value,
        promo : form['promo'].value, code : form['code'].value, lastchoose : form['lastchoose'].value },
       function(html){
        $("#chooseAdd div.content").html(html);	
      }, "html");
    }else{
      $.post("/personal/items/add/?promo="+promo+"&code="+code, function(html){
        OpenModalWindow2('chooseAdd');
        $("#chooseAdd div.content").html(html);	
      }, "html");
    }
  }


Help! Where is the mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
horlon, 2019-05-10
@kopylov_vlad

This is the jamb of the browser developers.
request.open('POST', "/personal/items/add/", true);
Since "true" is an asynchronous connection, not a synchronous one, it turns out that the browser is swearing by mistake. Personally, this happens to me when the connection to the server where request.open is being hammered is simply not available, and if the server is available, then these errors do not exist.

V
Vladislav Kopylov, 2015-02-17
@kopylov_vlad

I rewrote the code to:

function GetAddFormAndOpenModal(form, promo, code){
  if(form){
    var params = 'itemadd=' + encodeURIComponent(form['itemadd'].value) 
      + '&submitform=' + encodeURIComponent(form['submitform'].value)
      + '&checkbutton=' + encodeURIComponent(form['checkbutton'].value)
      + '&promo=' + encodeURIComponent(form['promo'].value)
      + '&code=' + encodeURIComponent(form['code'].value)
      + '&lastchoose=' + encodeURIComponent(form['lastchoose'].value)
      ;
    var request = new XMLHttpRequest(); 
    request.onreadystatechange = processRequestChange;
    request.open('POST', "/personal/items/add/", true); 
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.send(params); 
  }else{
    var request = new XMLHttpRequest(); 
    request.onreadystatechange = processRequestChange;
    request.open('GET', "/personal/items/add/?promo="+promo+"&code="+code, true);
    request.send(null);
  }
}

Runs fine on chrome, but firefox still gives the error "Synchronous XMLHttpRequest on the main thread is deprecated due to its detrimental effect on the end user experience."
At the same time, I set request.open() to true - which means an asynchronous request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question