Executing a synchronous request using $ .ajax

we know that only synchronous requests are executed with $ .ajax with the async option: false But I saw in the jQuery manual that "synchronous requests can temporarily block the browser by disabling any actions while the request is active." I have some questions, such as

I saw several answers saying to use callback () How can I get jQuery to execute a synchronous rather than asynchronous Ajax request? . How can I implement this for my function.

In the function below, if the request fails or the response is not returned. How can I process it. I can use "error:" or something like this. How can I improve this feature effectively, I mean using callback and error handling. They say that (we can never get a mistake and success with a request)

function empvalidate() {
          var exists = false;             // default return value is false
          var empno = $("#empno").val();
          if (empno != '') {
            $.ajax({
              url: "emp.php",
              async: false,
              dataType: "json",
              data: {'param1': $("#param1").val(), 'empno': $("#empno").val()},
              success: function (data) {
                exists = data.status;     // set status of existence to outer variable
              }
            });
          }
          return exists;                  // return actual value
        }
+3
source share
3 answers

Your synchronized request will cause the user interface thread (and therefore the browser) to wait until the object XMLHttpRequestreturns (the view regardless of whether it was unsuccessful or successfully completed).

jQuery / , "" . /, .

, , . latencys , .. , , , .

, - ajax .

+2

async: false - :

function begin_ajax() {
    // Set your UI states here
}

function success_ajax(data) {
    // Run whatever needs to run if the request is successful.
}

function error_ajax(xhr, error_type, msg) {
    // Display error messages
}

function complete_ajax() {
    // Clean up UI
}
$.ajax({
    beforeSend: begin_ajax,
    success: success_ajax,
    error: error_ajax,
    complete: complete_ajax
    // Your parameters here
});
0

""

jQuery Docs

(XMLHttpRequest, textStatus) -

, ( ). : XMLHttpRequest , ( "", "notmodified", "error", "timeout" "Parsererror" ). Ajax.

ajax:

function empvalidate() {
      var exists = false;             // default return value is false
      var empno = $("#empno").val();
      if (empno != '') {
        $.ajax({
          url: "emp.php",
          async: false,
          dataType: "json",
          data: {'param1': $("#param1").val(), 'empno': $("#empno").val()},
          success: function (data, ts) {

              if(ts == "success")
                   exists = data.status; // set status of existence to outer variable
              else if(ts == "timeout")
              {
                   $(document).trigger('customTimeoutHandler');
              }
          }
        });
      }
      return exists;                  // return actual value
    }

, , -. , , .

0

Source: https://habr.com/ru/post/1777707/


All Articles