Skip to content

Instantly share code, notes, and snippets.

@tvandervossen
Created February 12, 2012 21:05
Show Gist options
  • Save tvandervossen/1810845 to your computer and use it in GitHub Desktop.
Save tvandervossen/1810845 to your computer and use it in GitHub Desktop.
Quick patch for JSONP error callback support in Zepto
diff --git a/src/ajax.js b/src/ajax.js
index d75e13c..f9d399f 100644
--- a/src/ajax.js
+++ b/src/ajax.js
@@ -99,6 +99,11 @@
},
xhr = { abort: abort }, abortTimeout;
+ if (options.error) script.onerror = function() {
+ xhr.abort();
+ options.error();
+ };
+
window[callbackName] = function(data){
clearTimeout(abortTimeout);
$(script).remove();
diff --git a/test/ajax.html b/test/ajax.html
index fc0dd8d..5d6e3b5 100644
--- a/test/ajax.html
+++ b/test/ajax.html
@@ -118,6 +118,16 @@
t.assertEqual(1, $('script[src^=fixtures]').size());
t.assertIn('abort', xhr);
},
+
+ testAjaxGetJSONPErrorCallback: function(t){
+ var successFired, errorFired, xhr = $.ajax({
+ type: 'GET',
+ url: 'fixtures/404.js?callback=?&timestamp='+(+new Date),
+ dataType: 'jsonp',
+ success: function() { t.refute(true); },
+ error: function() { t.assert(true); }
+ });
+ },
testAjaxLoad: function(t) {
var testEl = $('#ajax_load');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment