Fixes #2994. Not finding a transport now fires the error callbacks and doesn't make...
[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 // Detect, normalize options and install callbacks for jsonp requests
15 }).ajaxPrefilter("json jsonp", function(s, originalSettings) {
16
17         if ( s.dataTypes[ 0 ] === "jsonp" ||
18                 originalSettings.jsonp ||
19                 originalSettings.jsonpCallback ||
20                 jsre.test(s.url) ||
21                 typeof(s.data) === "string" && jsre.test(s.data) ) {
22
23                 var responseContainer,
24                         jsonpCallback = s.jsonpCallback =
25                                 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
26                         previous = window[ 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
37                 window [ jsonpCallback ] = function( response ) {
38                         responseContainer = [response];
39                 };
40
41                 s.complete = [function() {
42
43                         // Set callback back to previous value
44                         window[ jsonpCallback ] = previous;
45
46                         // Call if it was a function and we have a response
47                         if ( previous) {
48                                 if ( responseContainer && jQuery.isFunction ( previous ) ) {
49                                         window[ jsonpCallback ] ( responseContainer[0] );
50                                 }
51                         } else {
52                                 // else, more memory leak avoidance
53                                 try{ delete window[ jsonpCallback ]; } catch(e){}
54                         }
55
56                 }, s.complete ];
57
58                 // Use data converter to retrieve json after script execution
59                 s.converters["script json"] = function() {
60                         if ( ! responseContainer ) {
61                                 jQuery.error( jsonpCallback + " was not called" );
62                         }
63                         return responseContainer[ 0 ];
64                 };
65
66                 // force json dataType
67                 s.dataTypes[ 0 ] = "json";
68
69                 // Delegate to script
70                 return "script";
71         }
72 });
73
74 })( jQuery );