Blame view

bower_components/jquery/src/ajax/jsonp.js 2.46 KB
c5169e0e   Renato De Donato   a new hope
1
  define([
74249687   Luigi Serra   Cross browser con...
2
3
4
5
6
7
8
9
10
11
  	"../core",
  	"./var/nonce",
  	"./var/rquery",
  	"../ajax"
  ], function( jQuery, nonce, rquery ) {
  
  var oldCallbacks = [],
  	rjsonp = /(=)\?(?=&|$)|\?\?/;
  
  // Default jsonp settings
c5169e0e   Renato De Donato   a new hope
12
  jQuery.ajaxSetup({
74249687   Luigi Serra   Cross browser con...
13
14
15
16
17
18
  	jsonp: "callback",
  	jsonpCallback: function() {
  		var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
  		this[ callback ] = true;
  		return callback;
  	}
c5169e0e   Renato De Donato   a new hope
19
  });
74249687   Luigi Serra   Cross browser con...
20
21
22
23
24
25
26
  
  // Detect, normalize options and install callbacks for jsonp requests
  jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
  
  	var callbackName, overwritten, responseContainer,
  		jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
  			"url" :
c5169e0e   Renato De Donato   a new hope
27
  			typeof s.data === "string" && !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data"
74249687   Luigi Serra   Cross browser con...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  		);
  
  	// Handle iff the expected data type is "jsonp" or we have a parameter to set
  	if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) {
  
  		// Get callback name, remembering preexisting value associated with it
  		callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
  			s.jsonpCallback() :
  			s.jsonpCallback;
  
  		// Insert callback into url or form data
  		if ( jsonProp ) {
  			s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
  		} else if ( s.jsonp !== false ) {
  			s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
  		}
  
  		// Use data converter to retrieve json after script execution
c5169e0e   Renato De Donato   a new hope
46
  		s.converters["script json"] = function() {
74249687   Luigi Serra   Cross browser con...
47
48
49
50
51
52
  			if ( !responseContainer ) {
  				jQuery.error( callbackName + " was not called" );
  			}
  			return responseContainer[ 0 ];
  		};
  
c5169e0e   Renato De Donato   a new hope
53
  		// force json dataType
74249687   Luigi Serra   Cross browser con...
54
55
56
57
58
59
60
61
62
  		s.dataTypes[ 0 ] = "json";
  
  		// Install callback
  		overwritten = window[ callbackName ];
  		window[ callbackName ] = function() {
  			responseContainer = arguments;
  		};
  
  		// Clean-up function (fires after converters)
c5169e0e   Renato De Donato   a new hope
63
64
65
  		jqXHR.always(function() {
  			// Restore preexisting value
  			window[ callbackName ] = overwritten;
74249687   Luigi Serra   Cross browser con...
66
67
68
  
  			// Save back as free
  			if ( s[ callbackName ] ) {
c5169e0e   Renato De Donato   a new hope
69
  				// make sure that re-using the options doesn't screw things around
74249687   Luigi Serra   Cross browser con...
70
71
  				s.jsonpCallback = originalSettings.jsonpCallback;
  
c5169e0e   Renato De Donato   a new hope
72
  				// save the callback name for future use
74249687   Luigi Serra   Cross browser con...
73
74
75
76
77
78
79
80
81
  				oldCallbacks.push( callbackName );
  			}
  
  			// Call if it was a function and we have a response
  			if ( responseContainer && jQuery.isFunction( overwritten ) ) {
  				overwritten( responseContainer[ 0 ] );
  			}
  
  			responseContainer = overwritten = undefined;
c5169e0e   Renato De Donato   a new hope
82
  		});
74249687   Luigi Serra   Cross browser con...
83
84
85
86
  
  		// Delegate to script
  		return "script";
  	}
c5169e0e   Renato De Donato   a new hope
87
  });
74249687   Luigi Serra   Cross browser con...
88
  
c5169e0e   Renato De Donato   a new hope
89
  });