带有异步的jQuery Ajax调用返回null responseXML [英] jQuery Ajax call with async returns null responseXML

查看:135
本文介绍了带有异步的jQuery Ajax调用返回null responseXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的JavaScript代码,该代码使用Ajax API从服务器中获取XML.

I have simple JavaScript code that is using the Ajax API for fetching a XML from the server.

function loadXML(path, node) {
    var response_obj = "";
    /* Fire Ajax request and get the XML */
    var request_obj = "";
    $.ajax({
        async: true,
        type: "GET",
        url: path,
        dataType: "xml",
        success: function(XMLObj, status, resquestObj) {
            response_obj = XMLObj;
            request_obj = requestObj;
        },
        error: function(){
            alert("Unable to fire AJAX request");
        }
    });
    alert(response_obj); //<-- This is NULL if async=true
   /* More logic to follow which will use response_obj (XML)
      and render it in the 'node' argument passed. */
}

其中的路径对应于 valid XML(使用 W3C验证程序进行了测试) ),并且该节点指向HTML页面上的DIV元素,必须在其中解析和附加响应.

Where, the path corresponds to a valid XML (tested using W3C validator) and the node points to a DIV element on the HTML page where the response has to be parsed and appended.

我需要的是Ajax调用应返回我要解析和呈现的responseXML对象.为此,我将响应XML分配给局部变量(此函数作用域的局部变量),然后将其使用(当前我只是将其发送给警报).

What I need is that the Ajax call should return me the responseXML object which I will parse and render. For that, I am assigning the response XML to a local variable (local to this function's scope) and then would use it (currently I just send it to alert).

现在,问题在于,当我在async设置为false的情况下使用此功能时,alert调用成功返回了[object XMLDocument].但是,一旦我将async更改为truenull就会由alert打印.

Now, the problem is that when I use this function with async set to false, the alert call successfully returns [object XMLDocument]. But as soon as I change async to true, null is printed by alert.

我了解的是,当async:true时,即使在Ajax调用返回XML之前,最后一个alert也会被调用.同样,当async:false时,这是一个序列化调用,当控制到达最后一个alert调用时,XML已经到达.

What I understand is that when async:true, the last alert is called even before the XML is returned by the Ajax call. Similarly, when async:false, this is a serialized call and when control reaches the last alert call, the XML has already arrived.

我该怎么做:

  1. 我有响应对象(和 请求对象),由Ajax返回 调用局部变量,以便 我可以使用它们.我明白那个 回调函数(如果传递给) success,将有三个 参数-但是,我不知道如何 从该回拨返回 功能进入我的loadXML函数.
  2. 我不必将Ajax调用转换为同步,因为这只是我要触发的功能之一-还有许多其他的Ajax调用也要发布.
  3. 我是否可以在此函数中序列化async:false而不是序列化async:true的假设?
  1. I have the response Object (and request object) as returned by the Ajax call in the local variables so that I can use them. I understand that the call back function, if passed to success, would have three parameters - but, I don't know how to return from that call back function into my loadXML function.
  2. That I don't have to convert the Ajax call into sync because this is just one of the functions that I am firing - there are many other Ajax calls which too are to be issued.
  3. Am I correct in my assumption that async:false is serialized in this function whereas async:true is not?

推荐答案

是的,您提到的有关async:true的内容是正确的.因此,如果async为true,而alert()自然会失败,而async为false,它将正常工作.

yes, what you have mentioned about async:true is correct. So it's natural that alert() will fail if async is true and if async is false it will work.

您应该将alert()放在成功回调中.

You should put the alert() in the success callback.

或者您也可以使函数说出ajaxSuccess并成功调用Callback(). 将alert()放入该ajaxSuccess函数中.

Or you can also make a function say ajaxSuccess and call it in success Callback(). Put the alert() in that ajaxSuccess function.

这篇关于带有异步的jQuery Ajax调用返回null responseXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆