Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回AJAX调用数据的JavaScript函数_Javascript_Ajax_Jquery - Fatal编程技术网

返回AJAX调用数据的JavaScript函数

返回AJAX调用数据的JavaScript函数,javascript,ajax,jquery,Javascript,Ajax,Jquery,我想创建一个JavaScript函数,它返回jQuery AJAX调用的值。我想要这样的东西 function checkUserIdExists(userid){ return $.ajax({ url: 'theurl', type: 'GET', cache: false, data: { userid: userid }, success: function(da

我想创建一个JavaScript函数,它返回jQuery AJAX调用的值。我想要这样的东西

function checkUserIdExists(userid){
    return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        },
        success: function(data){
            return data;
        }
    });
}

我知道我可以通过将async设置为false来实现这一点,但我宁愿不这样做。

这并不是JavaScript异步编程的真正目的。相反,在success函数中使用回调函数调用另一个函数以使用从服务器返回的数据。

您可以传入回调函数:

function checkUserIdExists(userid, callback) {
    $.ajax({
        ...
        success: callback
    });
}

checkUserIdExists(4, function(data) {

});

除非您希望同步调用AJAX调用,否则您不能返回AJAX调用返回的数据(而且您不希望,相信我)。但您可以返回的是AJAX调用返回的数据的承诺,并且您实际上可以以非常优雅的方式来完成

更新: 请注意,当前jQuery承诺与中的-more-info不兼容。)

基本上,您可以返回$.ajax(…)调用的返回值:

调用您的函数的人可以这样使用它:

checkUserIdExists(userid).success(function (data) {
    // do something with data
});

如果您感兴趣,请参阅以获得更好的解释和演示。

对于jQuery 1.5,您可以使用全新的功能,这正是为了实现这一点


蒂姆,这两种情况是相互排斥的;异步操作不能用于任何目的,也不能检索返回的数据

您应该为ajax调用查看一个支持事件的基础结构

从jQuery1.8开始,“success”、“error”和“complete”回调都不推荐使用。相反,您应该使用“完成”、“失败”和“始终”

所以你可以:

function checkUserIdExists(userid, callback) {
        return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        }
    })
    .done(callback)
    .fail(function(jqXHR, textStatus, errorThrown) {
        // Handle error
    });
}

checkUserIdExists(2, function(data) {
    console.log(data); // Do what you want with the data returned
});

+1-这是我的想法,但是在一个简洁的代码示例中,这里有一个重要的东西。您必须在
$.ajax
方法中添加
async:false
属性。如果没有,则不会将数据输入到
全局变量中。这是一个额外的信息。思考我所想的+1。我的答案的一个不那么懒惰的版本:)如何从这个变量jqxhr读取json数据
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
function checkUserIdExists(userid, callback) {
        return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        }
    })
    .done(callback)
    .fail(function(jqXHR, textStatus, errorThrown) {
        // Handle error
    });
}

checkUserIdExists(2, function(data) {
    console.log(data); // Do what you want with the data returned
});