Jquery $ .ajax () and its success callback - where does it return?

Where does jquery ajax success callback come back?


I wrote a request method that makes an ajax call, detects if I was provided with a callback or returns a datanode of an XML response.

Request Method:

function request(request, dontRefresh)
{     
   var requestXML = composeRequestXML(request); 
   $.ajax({
      url: 'someProcessor.php',
      type: 'POST',
      cache: false,
      async: dontRefresh,
      timeout: 5000,
      data: "query="+requestXML,
      success: function(response)
      {
         //parses xml into a js object
         var responseObj = parseResponseXML(response);

         if(request.callback){
            request.callback(responseObj);
         } else {
            // responseObj.response[0].data[0] is the data 
            // node of the response Obj.
            // this is what i see being the problem - 
            // I DON'T KNOW WHERE THIS IS RETURNED TO!!
            return responseObj.response[0].data[0];
         }
      }
   });
}

This request will use a callback

var requestObj = new Object();
    requestObj.callback = function(responseObj){someCallbackFunction(responseObj);};
    requestObj[0] = new Object();
    requestObj[0].module = "someModule";
    requestObj[0].action = "someModuleMethod";
request(requestObj);
//results are returned to the function someCallbackFunction()

This is an example of what I would like to accomplish.

var requestObj = new Object();
    requestObj[0] = new Object();
    requestObj[0].module = "userManager";
    requestObj[0].action = "GET_USERID";

var userId = request(requestObj, false); //make the request asynchronous

I tried to return the function $.ajax()... like this:

function request(request, dontRefresh){
   return $.ajax({/*...options...*/);
}

But this bypasses the xml parser that I designed and returns an XHR object. I would like to use this technique to register variables. So essentially ...

I will use this method with a callback or setting a variable with it.

+3
2

jquery, jquery . , , - , ajax. , ajax , , , !

ajax ( jquery):

var ajax=new XMLHttpRequest();
ajax.open("GET", url, false);
ajax.send(null);
return ajax.responseText;

. IE , . jquery: jQuery , Ajax?

+3

, . .

, , , .

  /* ... */
  success: function(response)
  {
     //parses xml into a js object
     var responseObj = parseResponseXML(response);

     if(request.callback){
        request.callback(responseObj);
     } else {
        /* do something with the data */
        setUserId(responseObj.response[0].data[0]);
     }
  }

setUserId() - , - , .

, , , responseObj.response[0].data[0], , .

+2

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


All Articles