2009-08-12 26 views
9

以下は、私が取り組んでいるjqueryスクリプトです。私はそれの非関連部分をすべて削除しました。jQueryで別のAJAX呼び出しの中でAJAX呼び出しを行うことはできますか?

あなたはajax呼び出しがあることがわかります。結果からif文があり、他の項目は取り除かれましたが、とにかく最初のajax呼び出しの結果からcaptchaが選択されています。

私は2番目のajax呼び出しを作成する必要があります。これは私の問題です。エラーはありませんが、2番目の応答を返さないようです。何か不足していますか?

<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

"エラー"コールバック関数をトラップしようとしましたか?エラー:function(XHR、textStatus){console.log(XHR); }?火かき棒で要求にピックアップ? – gnarf

+1

これは実際にAJAX呼び出しの中にあるわけではなく、 'success'関数は*呼び出しの後*です。 – Kobi

答えて

12

ajaxコールバックでは何も問題はありません。その後、

  • cbox_closedイベントに関数をバインドする

    1. Ajax呼び出し
    2. 呼び出しが成功したとリターンが「キャプチャ」:実際には、あなたも、あなたがここでやっていることである、ということをやっていませんcbox_closedイベントがトリガされたときにいくつかのものは、チェックするために、別のAJAX呼び出し

    が含まバウンド関数を呼び出す:
    を成功裏に返すサーバですか? (404、500エラーなどはありません)
    あなたはjson応答を予期しています。それはあなたがチェックしている{レスポンス: '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."); 
    } 
    
    関連する問題