Fixes #8054 by reverting feature enhancement 5812 (4920). Regexps no longer searches...
[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 "jsonp" + jsc++;
11         }
12 });
13
14 // Detect, normalize options and install callbacks for jsonp requests
15 jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, dataIsString /* internal */ ) {
16
17         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
33                 if ( s.jsonp !== false ) {
34                         url = url.replace( jsre, replace );
35                         if ( s.url === url ) {
36                                 if ( dataIsString ) {
37                                         data = data.replace( jsre, replace );
38                                 }
39                                 if ( s.data === data ) {
40                                         // Add callback manually
41                                         url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
42                                 }
43                         }
44                 }
45
46                 s.url = url;
47                 s.data = data;
48
49                 window[ jsonpCallback ] = function( response ) {
50                         responseContainer = [ response ];
51                 };
52
53                 s.complete = [ function() {
54
55                         // Set callback back to previous value
56                         window[ jsonpCallback ] = previous;
57
58                         // Call if it was a function and we have a response
59                         if ( previous) {
60                                 if ( responseContainer && jQuery.isFunction( previous ) ) {
61                                         window[ jsonpCallback ] ( responseContainer[ 0 ] );
62                                 }
63                         } else {
64                                 // else, more memory leak avoidance
65                                 try{
66                                         delete window[ jsonpCallback ];
67                                 } catch( e ) {}
68                         }
69
70                 }, s.complete ];
71
72                 // Use data converter to retrieve json after script execution
73                 s.converters["script json"] = function() {
74                         if ( ! responseContainer ) {
75                                 jQuery.error( jsonpCallback + " was not called" );
76                         }
77                         return responseContainer[ 0 ];
78                 };
79
80                 // force json dataType
81                 s.dataTypes[ 0 ] = "json";
82
83                 // Delegate to script
84                 return "script";
85         }
86 } );
87
88 })( jQuery );