2010-07-29 24 views
5

साथ निपटने मैं इस तरह jQuery के AJAX समारोह के चारों ओर एक आवरण समारोह का उपयोग कर रहा:jQuery ajax त्रुटि "स्क्रिप्ट" डेटाप्रकार

$.getAjax = function(url, type, callback){ 
    $.ajax({ 
     url: url, 
     cache: false, 
     dataType: type, 

     success: function(){ 
      alert("success"); 
     }, 
     complete: function(XMLHttpRequest, textStatus){ 
      alert("complete"); 

      if (callback != undefined) { 
       callback(); 
      } 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown){ 
      alert("error"); 
     } 
    }); 
} 

जब मैं एक डेटाप्रकार के रूप में "पाठ" के साथ इस का उपयोग यह है, तो पूरी तरह से भी काम करता है यूआरएल अवैध है। जब एक यूआरएल अमान्य है तो यह पहले त्रुटि को पूर्ण कार्य करता है। ठीक है। लेकिन जब मैं डेटा स्क्रिप्ट के रूप में "स्क्रिप्ट" का उपयोग करता हूं तो यूआरएल अमान्य होने पर यह कुछ भी कॉल नहीं करता है। जब मैं डेटा स्क्रिप्ट के रूप में "स्क्रिप्ट" का उपयोग करता हूं तो HTTP 404 त्रुटियों और अन्य को पकड़ने के लिए मैं क्या करूँ?

+0

डीबग करने के लिए आप क्या उपयोग कर रहे हैं? क्या आपके पास फिडलर या फायरबग तक पहुंच है ताकि आप इसका आकलन कर सकें कि क्या अनुरोध किया जा रहा है और लौटाया गया है? एक और सवाल यह है: क्या आप अपने 'स्क्रिप्ट' अनुरोध के साथ किसी भिन्न डोमेन पर संसाधन तक पहुंचने का प्रयास कर रहे हैं? –

+0

मैं फायरबग भी कोशिश नहीं करता लेकिन यह एक अच्छा विचार है। संसाधन एक अलग डोमेन पर है, लेकिन मैं "क्रॉस डोमेन" से एक स्क्रिप्ट प्राप्त कर सकता हूं – Pink

+0

क्या होता है जब आप 'टाइप' पैरामीटर को 'स्क्रिप्ट' से 'jsonp' में बदलते हैं? क्या आपकी सफलता आग लगती है? –

उत्तर

1

मैंने jQuery के स्रोत को देखा है और मैंने पाया है कि यह किसी भी त्रुटि हैंडलर विधि को कॉल नहीं करता है। वास्तव में, यह केवल सफलता() और पूर्ण() कार्यों को कॉल करता है जब http प्राप्त अनुरोध सफल होता है।

// If we're requesting a remote document 
     // and trying to load JSON or Script with a GET 
     if (s.dataType === "script" && type === "GET" && remote) { 
      var head = document.getElementsByTagName("head")[0] || document.documentElement; 
      var script = document.createElement("script"); 
      script.src = s.url; 
      if (s.scriptCharset) { 
       script.charset = s.scriptCharset; 
      } 

      // Handle Script loading 
      if (!jsonp) { 
       var done = false; 

       // Attach handlers for all browsers 
       script.onload = script.onreadystatechange = function() { 
        if (!done && (!this.readyState || 
          this.readyState === "loaded" || this.readyState === "complete")) { 
         done = true; 
         success(); 
         complete(); 

         // Handle memory leak in IE 
         script.onload = script.onreadystatechange = null; 
         if (head && script.parentNode) { 
          head.removeChild(script); 
         } 
        } 
       }; 
      } 

      // Use insertBefore instead of appendChild to circumvent an IE6 bug. 
      // This arises when a base node is used (#2709 and #4378). 
      head.insertBefore(script, head.firstChild); 

      // We handle everything using the script element injection 
      return undefined; 
     } 
0

बुरी खबर: यह समस्या अभी भी jQuery 1.x. में हल नहीं होती है अच्छी खबर: यह jquery 2.x में हल किया गया है (जो IE < = 8 का समर्थन नहीं करता है)। मैंने अपना complete कॉलबैक काम करने में कामयाब रहा है, जो एक त्रुटि अधिसूचना प्राप्त करता है। Jquery कोड, या संबंधित स्निपेट के लिए here देखें:

send: function(_, complete) { 
    script = jQuery("<script>").prop({ 
     async: true, 
     charset: s.scriptCharset, 
     src: s.url 
    }).on(
     "load error", 
     callback = function(evt) { 
      script.remove(); 
      callback = null; 
      if (evt) { 
       complete(evt.type === "error" ? 404 : 200, evt.type); 
      } 
     } 
    ); 
    document.head.appendChild(script[ 0 ]); 
},