返回JQuery ajax调用的值

时间:2022-10-08 07:40:33

I want this function to return wether or not the ajax call was succesful or not. Is there any way I can do this? My code below doesn't do this.

我希望这个函数能够返回,或者不是ajax调用成功与否。有什么方法可以做到这一点吗?我的代码不会这样做。

function myFunction(data) {
var result = false;
$.ajax({
    type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: "url",
        data: data,
        error: function(data){
             result = false;
             return false;
        },
        success: function(data){
            result = true;
            return true;
        }
     });
     return result;
}

5 个解决方案

#1


5  

Unfortunately, you cannot return values to functions that wrap asynchronous callbacks. Instead, your success callback from the AJAX request will handoff the data and control to another function. I've demonstrated this concept below:

不幸的是,您无法将值返回到包装异步回调的函数。相反,您从AJAX请求中成功回调将数据和控制权移交给另一个函数。我在下面演示了这个概念:

Definition for myFunction:

myFunction的定义:

// I added a second parameter called "callback", which takes a function
 // as a first class object
function myFunction(data, callback) {
    var result = false;
    $.ajax({
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: "url",
        data: data,
        error: function(data){
            result = false;

            // invoke the callback function here
            if(callback != null)  {
                callback(result);
            }

            // this would return to the error handler, which does nothing
            //return false;
        },
        success: function(data){
            result = true;

            // invoke your callback function here
            if(callback != null) {
                callback(result);                
            }

            // this would actually return to the success handler, which does
              // nothing as it doesn't assign the value to anything
            // return true;
        }
     });

     // return result; // result would be false here still
}

callback function definition:

回调函数定义:

// this is the definition for the function that takes the data from your
 // AJAX success handler
function processData(result) {

    // do stuff with the result here

}

invoke your myFunction:

调用你的myFunction:

var data = { key: "value" }; /* some object you're passing in */

// pass in both the data as well as the processData function object
 // in JavaScript, functions can be passed into parameters as arguments!
myFunction(data, processData);

#2


2  

No need call back. You can achieve this using async property.

无需回电。您可以使用async属性来实现此目的。

function myFunction(){
    var retVal;
    $.ajax({
        url:url,
        method: GET/POST,
        data: data,
        async: false,
        success:function(response) {
            retVal = response;
        }
    });
    return retVal;
}

#3


1  

You can specify async: false in the AJAX configuration, though the documentation notes that this will also lock the browser for the duration of the AJAX call, so it is not recommended.

您可以在AJAX配置中指定async:false,但文档指出这也会在AJAX调用期间锁定浏览器,因此不建议这样做。

#4


1  

add this property that's help u

添加这个帮助你的属性

async: false

#5


0  

No, myFunction can't return the success of the ajax call, since the ajax call is done asynchronously.

不,myFunction无法返回ajax调用的成功,因为ajax调用是异步完成的。

You code will be executed in this order:

您的代码将按以下顺序执行:

  1. var result = false;
  2. var result = false;
  3. $.ajax sends the request to the server.
  4. $ .ajax将请求发送到服务器。
  5. return result (which is still set to false).
  6. 返回结果(仍设置为false)。
  7. When the response is received from the server, the success or error handler which contains result = false or result = true is called.
  8. 从服务器收到响应时,将调用包含result = false或result = true的成功或错误处理程序。

The correct way to handle this is to move any code that depends on the outcome of the ajax code into the success and error functions.

处理此问题的正确方法是将任何依赖于ajax代码结果的代码移动到成功和错误函数中。

#1


5  

Unfortunately, you cannot return values to functions that wrap asynchronous callbacks. Instead, your success callback from the AJAX request will handoff the data and control to another function. I've demonstrated this concept below:

不幸的是,您无法将值返回到包装异步回调的函数。相反,您从AJAX请求中成功回调将数据和控制权移交给另一个函数。我在下面演示了这个概念:

Definition for myFunction:

myFunction的定义:

// I added a second parameter called "callback", which takes a function
 // as a first class object
function myFunction(data, callback) {
    var result = false;
    $.ajax({
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: "url",
        data: data,
        error: function(data){
            result = false;

            // invoke the callback function here
            if(callback != null)  {
                callback(result);
            }

            // this would return to the error handler, which does nothing
            //return false;
        },
        success: function(data){
            result = true;

            // invoke your callback function here
            if(callback != null) {
                callback(result);                
            }

            // this would actually return to the success handler, which does
              // nothing as it doesn't assign the value to anything
            // return true;
        }
     });

     // return result; // result would be false here still
}

callback function definition:

回调函数定义:

// this is the definition for the function that takes the data from your
 // AJAX success handler
function processData(result) {

    // do stuff with the result here

}

invoke your myFunction:

调用你的myFunction:

var data = { key: "value" }; /* some object you're passing in */

// pass in both the data as well as the processData function object
 // in JavaScript, functions can be passed into parameters as arguments!
myFunction(data, processData);

#2


2  

No need call back. You can achieve this using async property.

无需回电。您可以使用async属性来实现此目的。

function myFunction(){
    var retVal;
    $.ajax({
        url:url,
        method: GET/POST,
        data: data,
        async: false,
        success:function(response) {
            retVal = response;
        }
    });
    return retVal;
}

#3


1  

You can specify async: false in the AJAX configuration, though the documentation notes that this will also lock the browser for the duration of the AJAX call, so it is not recommended.

您可以在AJAX配置中指定async:false,但文档指出这也会在AJAX调用期间锁定浏览器,因此不建议这样做。

#4


1  

add this property that's help u

添加这个帮助你的属性

async: false

#5


0  

No, myFunction can't return the success of the ajax call, since the ajax call is done asynchronously.

不,myFunction无法返回ajax调用的成功,因为ajax调用是异步完成的。

You code will be executed in this order:

您的代码将按以下顺序执行:

  1. var result = false;
  2. var result = false;
  3. $.ajax sends the request to the server.
  4. $ .ajax将请求发送到服务器。
  5. return result (which is still set to false).
  6. 返回结果(仍设置为false)。
  7. When the response is received from the server, the success or error handler which contains result = false or result = true is called.
  8. 从服务器收到响应时,将调用包含result = false或result = true的成功或错误处理程序。

The correct way to handle this is to move any code that depends on the outcome of the ajax code into the success and error functions.

处理此问题的正确方法是将任何依赖于ajax代码结果的代码移动到成功和错误函数中。