Checking JQuery AJAX Request Status

I make 2 different ajax requests via jQuery and I have to check if the other is active or not. How can i do this?

one example from my ajax requests:

active_project_categories_ajax = $.ajax(
{
    url: "/ajax/get_skill_list",
    dataType: 'json',
    ......
});

I need something like this: active_project_categories_ajax.status ()

+3
source share
3 answers

since you are returning an XMLHttpRequest object, you can always look

active_project_categories_ajax.readyState
active_project_categories_ajax.status

for ReadyState there must be 4 to complete (success or error). therefore, if it is less than 4, it is still active.

this is readyState:

// states
  const unsigned short UNSENT = 0;
  const unsigned short OPENED = 1;
  const unsigned short HEADERS_RECEIVED = 2;
  const unsigned short LOADING = 3;
  const unsigned short DONE = 4;

quote from: http://www.w3.org/TR/XMLHttpRequest/#the-xmlhttprequest-interface

, ReadyState 4. . ( , php, 1MB ... readyState 3, 200. , 200, ReadyState 2).

+5

, AJAX :

var status;

$.ajax({
   beforeSend: function(){
     status = 1;
   },
   complete: function(){
     status = 2;
   }
   // ...
});
+3

You are almost there, statusa property XMLHttpRequest, not a function. jQuery.ajaxreturns an object XMLHttpRequest. Hold on to this, as you already do:

var req = $.ajax(..);

Then call statuson this object if necessary, and notstatus()

req.status
+1
source

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


All Articles