2009-08-12 14 views
9

नीचे एक jQuery स्क्रिप्ट मैं पर काम कर रहा हूँ है, मैं यह सब के गैर प्रासंगिक भागों नीचे छीन लिया है।क्या आप jquery में किसी अन्य AJAX कॉल के अंदर 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

क्या आपने "त्रुटि" कॉलबैक फ़ंक्शन को फँसाने का प्रयास किया है? त्रुटि: फ़ंक्शन (एक्सएचआर, टेक्स्टस्टैटस) {console.log (XHR); }? फायरबग के अनुरोध पर peeked? – gnarf

+1

यह वास्तव में * अंदर * AJAX कॉल नहीं है, 'सफलता' फ़ंक्शन * कॉल के बाद * है। – Kobi

उत्तर

12

AJAX कॉलबैक के अंदर AJAX कॉल करने में कुछ भी गलत नहीं है। वास्तव में, आप भी नहीं कर रहे हैं कि, क्या तुम यहाँ क्या कर रहे है:

  1. अजाक्स कॉल
  2. कॉल सफल है और रिटर्न 'कैप्चा' तो cbox_closed घटना के लिए एक समारोह के लिए बाध्य हैं
  3. cbox_closed घटना शुरू हो रहा है जब बाध्य समारोह के लिए जाँच करने के लिए एक और ajax कॉल

कुछ बातें शामिल हैं जो कहते हैं:
Sever सफलतापूर्वक लौट रहा है? (कोई 404, 500 त्रुटि आदि)
आप एक जेसन प्रतिक्रिया की उम्मीद कर रहे हैं। क्या इसमें {प्रतिक्रिया: 'कैप्चा} जैसे डेटा शामिल हैं जिन्हें आप जांच रहे हैं?
cbox_closed ईवेंट कब ट्रिगर किया गया है? क्या आप वाकई हो रहे हैं?

3

आप jQuery के लिए 'सफलता' कॉलबैक उत्तीर्ण कर ली है, लेकिन कोई 'त्रुटि' कॉलबैक। इस तरह आप त्रुटि को अनदेखा कर रहे हैं। त्रुटि को कैप्चर करने के लिए यहां एक उदाहरण दिया गया है, और इसे वैकल्पिक रूप से फ़ायरबग कंसोल में प्रदर्शित करें।

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."); 
} 
संबंधित मुद्दे