如何在 ajax 回调函数之外使用 xml 响应作为 XMLObject

发布于 2024-08-31 03:22:07 字数 572 浏览 1 评论 0原文

希望我只是犯了一个愚蠢的疏忽,但我不明白为什么以下不起作用:

$(function() {
var xml;
    $.get(
        "somexml.xml",
        function(data){
            xml = data;
        },
        "xml");
    alert(xml);
});

如果我将警报放在回调函数中,我会返回 object XMLdocument 但如果我将它放在 ajax 调用之外,我得到未定义

由于我的目标是要解析一个新的 DOM,因此我不希望 XML 文档的整个处理都在回调函数内。

我尝试在整个 onready 函数外部、顶部(如上)和回调函数内部定义变量,但都没有成功。

根据jquery文档中的指定Ajax请求的数据类型,这应该是可能的。

Hopefully I've just made a dumb oversight, but I can't figure out why the following doesn't work:

$(function() {
var xml;
    $.get(
        "somexml.xml",
        function(data){
            xml = data;
        },
        "xml");
    alert(xml);
});

If I put the alert inside of the callback function, I get back object XMLdocument but if I place it outside of the ajax call, I get undefined.

Since my goal is to have a new DOM to parse, I don't want the entire handling of the XMLdocument to be within the callback function.

I've tried defining the variable outside of the entire onready function, inside at the top (like above) and inside the callback function, all with no luck.

According to Specifying the Data Type for Ajax Requests in the jquery documentation, this should be possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-09-07 03:22:07

AJAX 被定义为异步 JavaScript 和 XML —— 尽管人们也使用该术语来表示同步和非 XML 请求。然而,异步意味着请求将在单独的线程上执行。 xml 未定义,因为发生了以下情况:

请求已发送 -> xml var 被访问 ->收到回复-> data 被分配给 xml 变量

如您所见,接收回的数据直到您尝试访问之后才分配给 xml 变量它。如果您只需要一个单独的块来处理 XML 数据,为什么不创建一个单独的函数并将其作为参数传递呢?

$(function() {
    $.get(
        "ews_cal_finditem.xml",
        parseXML,
        "xml");
});
function parseXML(data) {
    // This is the callback function
    alert(data);
}

甚至

$(function() {
    $.get(
        "ews_cal_finditem.xml",
        function(data){
            parseXML(data);
        },
        "xml");
});
function parseXML(xml) {
    alert(xml);
}

AJAX is defined as Asynchronous JavaScript and XML -- although people use the term for synchronous and non-XML requests too. Nevertheless, asynchronous means that the request will execute on a separate thread. xml is undefined because the following is happening:

Request is sent -> xml var is accessed -> response is received -> data is assigned to xml variable

As you can see, the data received back isn't assigned to the xml var until after you tried to access it. If you just want a separate block for handling the XML data, why not create a separate function and pass it as a parameter?

$(function() {
    $.get(
        "ews_cal_finditem.xml",
        parseXML,
        "xml");
});
function parseXML(data) {
    // This is the callback function
    alert(data);
}

Or even

$(function() {
    $.get(
        "ews_cal_finditem.xml",
        function(data){
            parseXML(data);
        },
        "xml");
});
function parseXML(xml) {
    alert(xml);
}
上课铃就是安魂曲 2024-09-07 03:22:07

您还可以以 async=false 的方式运行 ajax 调用,但应谨慎执行此操作,因为它将阻止进一步执行,直到完成为止。从 AJAX 中取出 A 时必须小心。

You could also run the ajax call as async=false, but this should be done carefully as it will block further execution until it's complete. You have to be careful when you take the A out of AJAX.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文