如何将jQuery.ajax()中的成功和错误处理程序中的值返回到父方法?

时间:2021-02-26 20:14:17

Ok, for sake of argument, let's say that the AJAX call MUST BE ASYNCHRONOUS.

好吧,为了论证,让我们说AJAX调用必须是异步的。

Basically, I have a jQuery AJAX call, and I want the success and error handlers to return a value to the parent method:

基本上,我有一个jQuery AJAX调用,我希望成功和错误处理程序返回一个值到父方法:

The code below does not work, since the AJAX call is asynchronous. Thus, the return value at the bottom of the function is returned before the repsonse is received:

由于AJAX调用是异步的,因此下面的代码不起作用。因此,在收到repsonse之前返回函数底部的返回值:

var isValid = false;

$.ajax({
    type: "POST",
    url: myurl,
    dataType: "JSON",
    data: $myData,
    async: true,
    success: function(data) {
        isValid = true;
    },
    error: function() {
        isValid = false;
    }
});

return isValid;

So, is there some way to pass a value BACK to the calling method for asynchronous AJAX calls?

那么,有没有办法将值BACK传递给异步AJAX调用的调用方法?

4 个解决方案

#1


1  

The line

这条线

Return isValid; 

Is going to execute before the call completes, because it's asynchronous.

将在调用完成之前执行,因为它是异步的。

Use

使用

async: false,

#2


1  

Here's a solution I have come up with that answers my question.... (Thanks to @Rodaine for giving me the idea!)

这是我提出的一个解决我的问题的解决方案......(感谢@Rodaine给我的想法!)

In the calling method, I am specifying a callback function. Here is the plug-in that contains the AJAX call:

在调用方法中,我指定了一个回调函数。这是包含AJAX调用的插件:

function ajaxCall(options) {
    var url = options["url"] === undefined ? "" : options["url"];
    var addLink = options["addLink"] === undefined ? "" : options["addLink"];

    var $form = $(this).closest("form");
    var $formParamsString = $form.serialize();

    var aSuccess = [];
    var aError = [];

    if ($.isFunction(options["success"])) 
        aSuccess.push(options["success"]);

    if ($.isFunction(options["error"])) 
        aError.push(options["error"]);

    $.ajax({
        type: "POST",
        url: url,
        dataType: "JSON",
        data: $formParamsString,
        async: true, // Asynchronous to allow for loading image
        success: aSuccess,
        error: aError
    });
}

Now, when I call this function, I can specify a callback in the options Object:

现在,当我调用此函数时,我可以在选项Object中指定一个回调:

ajaxCall({
    url: "myWebService.cfm",
    success: function() {
        alert("We did it!");
    },
    error: function() {
        alert("Try again, fool!");
    }
});

#3


0  

You can't do this because isValid would be always false.

你不能这样做,因为isValid总是假的。

If you need to do something following success/failure you should do:

如果您需要在成功/失败后做一些事情,您应该:

success: function(data) {
    isValid = true;
    otherFunction(isValid);
},
error: function() {
    isValid = false;
    otherFunction(isValid);
}

In this way you could handle the different cases

通过这种方式,您可以处理不同的情况

#4


-1  

Use synchronouse ajax call

使用synchronouse ajax调用

set async to false.

将async设置为false。

#1


1  

The line

这条线

Return isValid; 

Is going to execute before the call completes, because it's asynchronous.

将在调用完成之前执行,因为它是异步的。

Use

使用

async: false,

#2


1  

Here's a solution I have come up with that answers my question.... (Thanks to @Rodaine for giving me the idea!)

这是我提出的一个解决我的问题的解决方案......(感谢@Rodaine给我的想法!)

In the calling method, I am specifying a callback function. Here is the plug-in that contains the AJAX call:

在调用方法中,我指定了一个回调函数。这是包含AJAX调用的插件:

function ajaxCall(options) {
    var url = options["url"] === undefined ? "" : options["url"];
    var addLink = options["addLink"] === undefined ? "" : options["addLink"];

    var $form = $(this).closest("form");
    var $formParamsString = $form.serialize();

    var aSuccess = [];
    var aError = [];

    if ($.isFunction(options["success"])) 
        aSuccess.push(options["success"]);

    if ($.isFunction(options["error"])) 
        aError.push(options["error"]);

    $.ajax({
        type: "POST",
        url: url,
        dataType: "JSON",
        data: $formParamsString,
        async: true, // Asynchronous to allow for loading image
        success: aSuccess,
        error: aError
    });
}

Now, when I call this function, I can specify a callback in the options Object:

现在,当我调用此函数时,我可以在选项Object中指定一个回调:

ajaxCall({
    url: "myWebService.cfm",
    success: function() {
        alert("We did it!");
    },
    error: function() {
        alert("Try again, fool!");
    }
});

#3


0  

You can't do this because isValid would be always false.

你不能这样做,因为isValid总是假的。

If you need to do something following success/failure you should do:

如果您需要在成功/失败后做一些事情,您应该:

success: function(data) {
    isValid = true;
    otherFunction(isValid);
},
error: function() {
    isValid = false;
    otherFunction(isValid);
}

In this way you could handle the different cases

通过这种方式,您可以处理不同的情况

#4


-1  

Use synchronouse ajax call

使用synchronouse ajax调用

set async to false.

将async设置为false。