JQuery的Ajax调用的返回值 [英] Return value of JQuery ajax call

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

问题描述

我想这个函数返回羯羊或不Ajax调用是成功的与否。有没有什么办法可以做到这一点?下面我code没有做到这一点。

 函数myFunction的(数据){
VAR的结果= FALSE;
$阿贾克斯({
    键入:POST,
        的contentType:应用/ JSON
        数据类型:JSON,
        网址:URL,
        数据:数据,
        错误:功能(数据){
             结果= FALSE;
             返回false;
        },
        成功:功能(数据){
            结果=真;
            返回true;
        }
     });
     返回结果;
}
 

解决方案

不幸的是,你不能返回值的包装异步回调函数。相反,从AJAX请求你的成功回调将换手的数据和控制到另一个功能。我已经证明了下面这个概念:

的myFunction的定义:

  //我加了第二个参数被称为回调,这需要一个功能
 //作为第一类对象
函数myFunction的(数据,回调){
    VAR的结果= FALSE;
    $阿贾克斯({
        键入:POST,
        的contentType:应用/ JSON
        数据类型:JSON,
        网址:URL,
        数据:数据,
        错误:功能(数据){
            结果= FALSE;

            //这里调用回调函数
            如果(回调!= NULL){
                回调(结果);
            }

            //这将返回该错误处理程序,它不执行任何
            //返回false;
        },
        成功:功能(数据){
            结果=真;

            //这里调用回调函数
            如果(回调!= NULL){
                回调(结果);
            }

            //这实际上返回成功处理程序,它不
              //没有什么,因为它没有赋值到任何东西
            //返回true;
        }
     });

     //返回结果; //结果是假的这里还是
}
 

的回调函数定义:

  //这是从获取数据的函数的定义您
 // AJAX的成功处理程序
功能过程数据(结果){

    //做的东西,结果在这里

}
 

调用你的myFunction的:

  VAR数据= {关键:值}; / *你传递的*某些对象/

//通过在这两个数据以及过程数据功能对象
 //在JavaScript中,函数可以传递到参数,参数!
myFunction的(数据,过程数据);
 

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.

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;
}

解决方案

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:

Definition for 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:

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);

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

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