可能重复:
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;
}
这实际上只是我想要返回函数成功的最后一行:代码不起作用。为什么不?