Moved jQuery.ajax.prefilter and jQuery.ajax.transport to jQuery.ajaxPrefilter and...
[jquery.git] / src / ajax / jsonp.js
1 (function( jQuery ) {
2
3 var jsc = jQuery.now(),
4         jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i,
5         rquery_jsonp = /\?/;
6
7 // Default jsonp settings
8 jQuery.ajaxSetup({
9         jsonp: "callback",
10         jsonpCallback: function() {
11                 return "jsonp" + jsc++;
12         }
13
14 // Normalize jsonp queries
15 // 1) put callback parameter in url or data
16 // 2) sneakily ensure transportDataType is always jsonp for jsonp requests
17 }).ajaxPrefilter("json jsonp", function(s, originalSettings) {
18
19         if ( s.dataTypes[ 0 ] === "jsonp" ||
20                 originalSettings.jsonp ||
21                 originalSettings.jsonpCallback ||
22                 jsre.test(s.url) ||
23                 typeof(s.data) === "string" && jsre.test(s.data) ) {
24
25                 var jsonpCallback = s.jsonpCallback =
26                                 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
27                         url = s.url.replace(jsre, "$1" + jsonpCallback + "$2"),
28                         data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "$1" + jsonpCallback + "$2") : s.data;
29
30                 if ( url === s.url && data === s.data ) {
31                         url += (rquery_jsonp.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
32                 }
33
34                 s.url = url;
35                 s.data = data;
36                 s.dataTypes[ 0 ] = "jsonp";
37         }
38
39 // Bind transport to jsonp dataType
40 }).ajaxTransport("jsonp", function(s) {
41
42         // Put callback in place
43         var responseContainer,
44                 jsonpCallback = s.jsonpCallback,
45                 previous = window[ jsonpCallback ];
46
47         window [ jsonpCallback ] = function( response ) {
48                 responseContainer = [response];
49         };
50
51         s.complete = [function() {
52
53                 // Set callback back to previous value
54                 window[ jsonpCallback ] = previous;
55
56                 // Call if it was a function and we have a response
57                 if ( previous) {
58                         if ( responseContainer && jQuery.isFunction ( previous ) ) {
59                                 window[ jsonpCallback ] ( responseContainer[0] );
60                         }
61                 } else {
62                         // else, more memory leak avoidance
63                         try{ delete window[ jsonpCallback ]; } catch(e){}
64                 }
65
66         }, s.complete ];
67
68         // Sneakily ensure this will be handled as json
69         s.dataTypes[ 0 ] = "json";
70
71         // Use data converter to retrieve json after script execution
72         s.converters["script json"] = function() {
73                 if ( ! responseContainer ) {
74                         jQuery.error( jsonpCallback + " was not called" );
75                 }
76                 return responseContainer[ 0 ];
77         };
78
79         // Delegate to script transport
80         return "script";
81 });
82
83 })( jQuery );