1

可能重复:
jQuery:ajax调用成功后返回数据

我有以下从另一个函数调用的函数。它需要返回一些东西才能查看该函数是否有效。

//Function to close any existing connection 
function closeConnection(manual) {
    //Establish connection to php script 
    $.ajax({
        type: 'POST',
        url: 'action/chat/closeconnection.php',
        success: function (feedback) {
            //If this function was called manually, also empty chatTextDiv and fade out chatDiv
            if (manual == true) {
                //Stop getting messages
                clearInterval(setintervalid);
                //Fade out chatDiv
                $('#chatDiv').fadeOut(100);
                //Empty chatTextDiv
                $('#chatTextDiv').html('');
                //Fade in openChatDiv
                $('#openChatDiv').fadeIn(100);
            }
        }
    }).error(function () {
        //Append with connection error - user doesn't know that he is disconnected first so it is better to say this as the function will return false and stop other functions
        if (manual != true) {
            $('#chatTextDiv').append('You could not be connected. Please try again later.<hr/>');
            //Stop getting messages
            clearInterval(setintervalid);
            var closeconnectionsuccess = false;
        }
        elseif(manual == true) {
            $('#chatTextDiv').append('You could not be disconnected. Please try again later.<hr/>');
            var closeconnectionsuccess = true;
        }
    });

    return closeconnectionsuccess;
}

这实际上只是我想要返回函数成功的最后一行:代码不起作用。为什么不?

4

3 回答 3

3

正如@Rockitsauce 所说,您的 closeconnectionsuccess 变量不在函数的范围内,因此此时无法访问。然而,更重要的是,您必须考虑到您正在使用 jQuery 的异步函数 - 该函数将在调用回调 ( , )closeConnection之前完成执行。如果您想在 HTTP 请求完成后检索任何结果,您还必须向您自己的函数添加一个回调函数,从而使其也异步。successerror

因此,此代码应该可以工作:

//Function to close any existing connection 
function closeConnection(manual, callback) {
//Establish connection to php script 
$.ajax({
    type: 'POST',
    url: 'action/chat/closeconnection.php',
    success: function(feedback) {
        //If this function was called manually, also empty chatTextDiv and fade out chatDiv
        if(manual==true)
        {
            //Stop getting messages
            clearInterval(setintervalid);
            //Fade out chatDiv
            $('#chatDiv').fadeOut(100);
            //Empty chatTextDiv
            $('#chatTextDiv').html('');
            //Fade in openChatDiv
            $('#openChatDiv').fadeIn(100);
        }
    }
}).error(function() {
    //Append with connection error - user doesn't know that he is disconnected first so it is better to say this as the function will return false and stop other functions
    if(manual!=true)
    {
        $('#chatTextDiv').append('You could not be connected. Please try again later.<hr/>');
        //Stop getting messages
        clearInterval(setintervalid);
        callback(false);
    }
    elseif(manual==true)
    {
        $('#chatTextDiv').append('You could not be disconnected. Please try again later.<hr/>');
        callback(true);
    }
});
}

closeConnection(manual, function(success) {
    //Process result
});
于 2012-05-09T20:17:36.160 回答
1

您的 closeconnectionsuccess 变量超出范围。

于 2012-05-09T20:14:47.107 回答
1

ajax 调用是异步的。所以函数在 ajax 调用返回之前就结束了。

您要么必须传入并调用回调函数,要么在 ajax 调用完成时触发事件。

于 2012-05-09T20:16:41.713 回答