Permalink
Browse files

Merge JSONP support by deepsweet in to latest version of ajax.js

  • Loading branch information...
1 parent 5ecaa12 commit 0393df07659dfa97a5ee37359ef0d41adffaabd6 @mipearson mipearson committed Dec 20, 2010
Showing with 27 additions and 0 deletions.
  1. +18 −0 src/ajax.js
  2. +9 −0 test/ajax.html
View
@@ -1,8 +1,26 @@
(function($){
+
+ var jsonpID = 0;
+
function empty() {}
+
+ $.ajaxJSONP = function(options){
+ var jsonpString;
+ jsonpString = 'jsonp' + ++jsonpID;
+ window[jsonpString] = function(j){ options.success(j) }
+ var script = document.createElement('script');
+ $(script).attr({ src: options.url.replace(/callback=\?/, 'callback=' + jsonpString), type: 'text/javascript' });
+ $('head').append(script);
+ };
+
$.ajax = function(options){
// { type, url, data, success, dataType, contentType }
options = options || {};
+
+ if (options.url && /callback=\?/.test(options.url)) {
+ return $.ajaxJSONP(options)
+ }
+
var data = options.data,
callback = options.success || empty,
errback = options.error || empty,
View
@@ -62,6 +62,15 @@
});
});
},
+
+ testAjaxGetJSONP: function(t){
+ t.pause();
+ $.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?', function(data){
+ t.resume(function(){
+ this.assertEqual(data.items.length, 20);
+ });
+ });
+ },
testAjaxLoad: function(t) {
var testEl = $('#ajax_load');

0 comments on commit 0393df0

Please sign in to comment.