2009-08-12 52 views
9

下面是一個jQuery腳本我的工作,我已經剝離下來的這一切不相關的部分。你可以在jQuery中的另一個AJAX調用內部進行AJAX調用嗎?

你可以看到有一個ajax調用,從結果中有一個if語句,我們剝離了其他項目,但無論如何captcha從第一個ajax調用的結果中挑選出來。

然後我需要做第二個AJAX調用,這是我的問題是,我沒有得到任何錯誤,但它似乎並沒有返回的第二個響應,我失去的東西嗎?

<script type="text/javascript"> 
    $(document).ready(function() { 
     // make ajax call 
     var dataString = 'comment=' + comment; 
     $.ajax({ 
      type: "POST", 
      url: "process.php", 
      data: dataString, 
      dataType: "json", 
      success: function(data) { 
       //result from 1st ajax call returns "captcha" 
       if (data.response === 'captcha') { 
        //a bunch of code is ran 
        //////////////////////// 
        //then... 
        $().bind('cbox_closed', function() { 
         var dataString2 = 'comment=' + comment + '&run=captchagood'; 

         // our nested ajax call 
         // this is the part that is having trouble =(
         $.ajax({ 
          type: "POST", 
          url: "process.php", 
          data2: dataString2, 
          dataType: "json", 
          success: function(data2) { 
           if (data2.response === 'captchagood') { 
            alert('test'); 
            $('div#loader').find('img.load-gif').remove(); 
            $('div#loader').append('<span class="success">Thanks for your comment!</span>'); 
            $('div#loader').hide().fadeIn('slow'); 
            $('span.limit').remove(); 
            $('div#comments').append(data); 
            $('div#comments div.comment-unapproved:nth-child(1)').hide().slideDown('slow'); 
            $('input#submit-comment').unbind('click').click(function() { 
             return false; 
            }); 
            // success append the sanitized-comment to the page 
            $('#comments').prepend(data.comment); 
           }; 
           // end captcha status returned 
          } 
         }); 
        }); 
       }; 
       return false; 
      }); 
     }); 
    }); 
</script> 
+1

你試過捕獲「error」回調函數嗎?錯誤:函數(XHR,textStatus){console.log(XHR); }?用螢火蟲請求偷看? – gnarf 2009-08-12 06:43:31

+1

這是不是真的* *裏面的AJAX調用,'success'功能*後*呼叫。 – Kobi 2009-08-12 06:46:20

回答

12

在ajax回調中進行ajax調用沒有任何問題。事實上,你甚至不這樣做,你在這裏做的是:

  1. Ajax調用
  2. 如果調用成功,並返回「驗證碼」,那麼一個函數綁定到cbox_closed事件
  3. 當cbox_closed事件被觸發調用其中包含另一個Ajax調用

有些事情要檢查綁定功能:
是對服務器成功返回? (沒有404,500錯誤等)
你能預料的JSON響應。它是否包含您正在檢查的{response:'captcha}等數據?
當該cbox_closed事件觸發?你確定它發生了嗎?

3

你已經通過了「成功」回調jQuery的,但沒有「錯誤」回調。這樣你就忽略了錯誤。以下是捕獲錯誤的示例,並可選擇將其顯示在Firebug控制檯中。

var lastRequest = jQuery.ajax({ type:  "POST" 
           , url:  this._ajaxUrl 
           , data:  postdata 
           , dataType: "json" 
           , success: _gotAjaxResponse 
           , error: _gotAjaxError 
           }); 

function _gotAjaxResponse(data) 
{ 
    window.console && console.log(data); 
} 

function _gotAjaxError(xhr, status, error) 
{ 
    // Show server script errors in the console. 
    if(xhr.status == 500) 
    { 
    var response = xhr.responseText; 
    response = response.replace(/\r?\n/g, "") 
         .replace(/(<\/)/g, "\n$1") 
         .replace(/<[^>]+>/g, "") 
         .replace(/^\s+/, "") 
         .replace(/\s+$/, ""); 
    window.console && console.error(response); 
    } 

    alert("I'm sorry...\n\n" 
     + "Unfortunately an error occured while communicating with the server.\n" 
     + "Please try again later."); 
} 
相關問題