简体   繁体   English

jquery $ .ajax()及其成功回调 - 它返回到哪里?

[英]jquery $.ajax() and its success callback - where is it returning to?

Where does the jquery ajax success callback return to? jquery ajax成功回调在哪里返回?


I have written a request method that makes an ajax call, detects if I have supplied a callback or returns the datanode of the response XML. 我编写了一个请求方法来进行ajax调用,检测我是否提供了回调或返回响应XML的datanode。

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 would use the 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'd 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've tried returning the $.ajax() function itself... like so: 我试过返回$.ajax()函数本身......就像这样:

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

But this bypasses the xml parser I have developed and returns the XHR object. 但这会绕过我开发的xml解析器并返回XHR对象。 I would like to use this kind of technique to register variables. 我想用这种技术来记录变量。 So essentially... 基本上......

I will be using this method with a callback or setting a variable with it. 我将使用此方法进行回调或使用它设置变量。

It gets returned to jquery, and jquery discards it. 它返回到jquery,jquery丢弃它。 If you want to make your code stop until something has been loaded, use synchronous ajax. 如果要在加载某些内容之前停止代码,请使用同步ajax。 But that makes the page irresponsive until the ajax request is complete, so please don't do it! 但是这会使页面无法响应,直到ajax请求完成,所以请不要这样做!

Example of synchronous ajax (without jquery): 同步ajax的示例(不带jquery):

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

Note: For IE compatibility, this gets a little bit more complicated, this is just a proof of concept. 注意:对于IE兼容性,这有点复杂,这只是一个概念证明。 And this would be the jquery way of doing it: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request? 这将是jquery这样做的方式: 我如何让jQuery执行同步而非异步的Ajax请求?

From your perspective, the function does not return to anywhere. 从您的角度来看,该功能不会返回任何地方。 Return values are lost. 返回值丢失。

The key with asynchronous processing is that you handle any data associated with an event in the event handler, or pass it on from there. 异步处理的关键是您处理与事件处理程序中的事件关联的任何数据,或从那里传递它。

  /* ... */
  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]);
     }
  }

Where setUserId() is a function that does something useful, as soon as the user ID is available. 其中setUserId()是一个有用的函数,只要用户ID可用。

As an aside, you should probably not use responseObj.response[0].data[0] without making sure first that that value actually exists. responseObj.response[0].data[0] ,您可能不应该使用responseObj.response[0].data[0]而不确定该值是否实际存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM