2012-05-09 70 views
0

可能重复:
jQuery: Return data after ajax call successjQuery函数返回不起作用

我有以下函数从另一个函数调用。它需要返回一些信息以查看函数是否工作。

//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

总之现在:你不能用一个异步函数返回一个值。 – Blazemonger

+2

您有一个语法错误'elseif(manual == true)//“else if”with a space' – elclanrs

回答

3

正如@Rockitsauce所说,你的closeconnectionuccess变量不在函数的范围之内,因此在那个时候不能访问。但更重要的是,您必须考虑到您正在使用jQuery的异步函数 - 函数closeConnection将在调用回调函数(success,error)之前完成执行。如果要在HTTP请求完成后检索任何结果,则必须为自己的函数添加回调函数,从而使其也可以异步。

此代码应因此工作:

//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 
}); 
+0

这是正确的做法。 –

1

您的closeconnectionsuccess变量超出了范围。

+2

请解释。 – Blazemonger

+0

考虑添加回调函数,以便您可以在请求完成后执行操作。 –

1

ajax调用是异步的。所以函数在ajax调用返回之前到达结尾。

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