2012-05-09 93 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調用完成時觸發事件。