有没有办法从JQuery Ajax调用返回一个值(在我的情况下是xml)

时间:2022-10-09 11:09:09

I guess I am missing something quite fundamental and maybe someone can fill me in. I have used an ajax call from two places. So now I am trying to reuse that call by making the call return a value. It looks something like this:

我想我错过了一些非常基本的东西,也许有人可以填补我。我已经从两个地方使用了ajax调用。所以现在我试图通过使调用返回一个值来重用该调用。它看起来像这样:

function getInfo()
{
    $.ajax({
    type: "GET",
    url: "../ajax.aspx?action=getInfo&id=4", 
        dataType: "xml",
    async: false,
    error: function() {
        alert("Something went wrong.");
    }, 
    success: function(xml) {
            // Do some extra work here
    $(xml).find("room").each(function() {
        // Do something based on the xml
    }); 
    // Something else can use this XML so return it too.
            // Why does this return not work???
            return xml;
    } 
});
}

So somewhere else in the script i am calling that function

所以脚本中的其他地方我正在调用该函数

var xml = getInfo();
// Try do something with it now but it says that it is undefined

and when i say it says it is undefined I am talking about Firebug.

当我说它不明确时,我说的是Firebug。

1 个解决方案

#1


Turning off the asynchronous functionality is imho not a very good AJAX programming style. You will loose a lot of the advantages of this technology. From the jquery documentation:

关闭异步功能并不是一个非常好的AJAX编程风格。您将失去这项技术的许多优点。从jquery文档:

Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

请注意,同步请求可能会暂时锁定浏览器,并在请求处于活动状态时禁用任何操作。

If you need to do it anyway: $.ajax returns the XMLHTTPRequest object it creates. And your getInfo method should return that as well, so your code should be modified like this:

如果你还需要这样做:$ .ajax返回它创建的XMLHTTPRequest对象。并且你的getInfo方法也应该返回它,所以你的代码应该像这样修改:

function getInfo()
{
    return $.ajax({
    type: "GET",
    url: "../ajax.aspx?action=getInfo&id=4", 
        dataType: "xml",
    async: false,
    error: function() {
        alert("Something went wrong.");
    }, 
    success: function(xml) {
            // Do some extra work here
        $(xml).find("room").each(function() {
            // Do something based on the xml
        });     
        // Something else can use this XML so return it too.
            // Why does this return not work???
            return xml;
    } 
}).responseText;
}


var xml = getInfo();

#1


Turning off the asynchronous functionality is imho not a very good AJAX programming style. You will loose a lot of the advantages of this technology. From the jquery documentation:

关闭异步功能并不是一个非常好的AJAX编程风格。您将失去这项技术的许多优点。从jquery文档:

Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

请注意,同步请求可能会暂时锁定浏览器,并在请求处于活动状态时禁用任何操作。

If you need to do it anyway: $.ajax returns the XMLHTTPRequest object it creates. And your getInfo method should return that as well, so your code should be modified like this:

如果你还需要这样做:$ .ajax返回它创建的XMLHTTPRequest对象。并且你的getInfo方法也应该返回它,所以你的代码应该像这样修改:

function getInfo()
{
    return $.ajax({
    type: "GET",
    url: "../ajax.aspx?action=getInfo&id=4", 
        dataType: "xml",
    async: false,
    error: function() {
        alert("Something went wrong.");
    }, 
    success: function(xml) {
            // Do some extra work here
        $(xml).find("room").each(function() {
            // Do something based on the xml
        });     
        // Something else can use this XML so return it too.
            // Why does this return not work???
            return xml;
    } 
}).responseText;
}


var xml = getInfo();