Fixes #8115. Renames all references to jXHR with jqXHR in the code (like was done...
[jquery.git] / src / ajax / jsonp.js
1 (function( jQuery ) {
2
3 var jsc = jQuery.now(),
4         jsre = /(\=)\?(&|$)|()\?\?()/i;
5
6 // Default jsonp settings
7 jQuery.ajaxSetup({
8         jsonp: "callback",
9         jsonpCallback: function() {
10                 return jQuery.expando + "_" + ( jsc++ );
11         }
12 });
13
14 // Detect, normalize options and install callbacks for jsonp requests
15 jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
16
17         var dataIsString = ( typeof s.data === "string" );
18
19         if ( s.dataTypes[ 0 ] === "jsonp" ||
20                 originalSettings.jsonpCallback ||
21                 originalSettings.jsonp != null ||
22                 s.jsonp !== false && ( jsre.test( s.url ) ||
23                                 dataIsString && jsre.test( s.data ) ) ) {
24
25                 var responseContainer,
26                         jsonpCallback = s.jsonpCallback =
27                                 jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
28                         previous = window[ jsonpCallback ],
29                         url = s.url,
30                         data = s.data,
31                         replace = "$1" + jsonpCallback + "$2",
32                         cleanUp = function() {
33                                 // Set callback back to previous value
34                                 window[ jsonpCallback ] = previous;
35                                 // Call if it was a function and we have a response
36                                 if ( responseContainer && jQuery.isFunction( previous ) ) {
37                                         window[ jsonpCallback ]( responseContainer[ 0 ] );
38                                 }
39                         };
40
41                 if ( s.jsonp !== false ) {
42                         url = url.replace( jsre, replace );
43                         if ( s.url === url ) {
44                                 if ( dataIsString ) {
45                                         data = data.replace( jsre, replace );
46                                 }
47                                 if ( s.data === data ) {
48                                         // Add callback manually
49                                         url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
50                                 }
51                         }
52                 }
53
54                 s.url = url;
55                 s.data = data;
56
57                 // Install callback
58                 window[ jsonpCallback ] = function( response ) {
59                         responseContainer = [ response ];
60                 };
61
62                 // Install cleanUp function
63                 jqXHR.then( cleanUp, cleanUp );
64
65                 // Use data converter to retrieve json after script execution
66                 s.converters["script json"] = function() {
67                         if ( !responseContainer ) {
68                                 jQuery.error( jsonpCallback + " was not called" );
69                         }
70                         return responseContainer[ 0 ];
71                 };
72
73                 // force json dataType
74                 s.dataTypes[ 0 ] = "json";
75
76                 // Delegate to script
77                 return "script";
78         }
79 } );
80
81 })( jQuery );