Blame view

bower_components/jquery/src/ajax/xhr.js 4.16 KB
a1a3bc73   Luigi Serra   graphs updates
1
  define( [
74249687   Luigi Serra   Cross browser con...
2
3
4
5
6
7
8
  	"../core",
  	"../var/support",
  	"../ajax"
  ], function( jQuery, support ) {
  
  jQuery.ajaxSettings.xhr = function() {
  	try {
a1a3bc73   Luigi Serra   graphs updates
9
10
  		return new window.XMLHttpRequest();
  	} catch ( e ) {}
74249687   Luigi Serra   Cross browser con...
11
12
  };
  
a1a3bc73   Luigi Serra   graphs updates
13
14
15
  var xhrSuccessStatus = {
  
  		// File protocol always yields status code 0, assume 200
74249687   Luigi Serra   Cross browser con...
16
  		0: 200,
a1a3bc73   Luigi Serra   graphs updates
17
  
74249687   Luigi Serra   Cross browser con...
18
19
20
21
22
23
  		// Support: IE9
  		// #1450: sometimes IE returns 1223 when it should be 204
  		1223: 204
  	},
  	xhrSupported = jQuery.ajaxSettings.xhr();
  
74249687   Luigi Serra   Cross browser con...
24
25
26
  support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
  support.ajax = xhrSupported = !!xhrSupported;
  
a1a3bc73   Luigi Serra   graphs updates
27
28
  jQuery.ajaxTransport( function( options ) {
  	var callback, errorCallback;
74249687   Luigi Serra   Cross browser con...
29
30
31
32
33
34
  
  	// Cross domain only allowed if supported through XMLHttpRequest
  	if ( support.cors || xhrSupported && !options.crossDomain ) {
  		return {
  			send: function( headers, complete ) {
  				var i,
a1a3bc73   Luigi Serra   graphs updates
35
  					xhr = options.xhr();
74249687   Luigi Serra   Cross browser con...
36
  
a1a3bc73   Luigi Serra   graphs updates
37
38
39
40
41
42
43
  				xhr.open(
  					options.type,
  					options.url,
  					options.async,
  					options.username,
  					options.password
  				);
74249687   Luigi Serra   Cross browser con...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  
  				// Apply custom fields if provided
  				if ( options.xhrFields ) {
  					for ( i in options.xhrFields ) {
  						xhr[ i ] = options.xhrFields[ i ];
  					}
  				}
  
  				// Override mime type if needed
  				if ( options.mimeType && xhr.overrideMimeType ) {
  					xhr.overrideMimeType( options.mimeType );
  				}
  
  				// X-Requested-With header
  				// For cross-domain requests, seeing as conditions for a preflight are
  				// akin to a jigsaw puzzle, we simply never set it to be sure.
  				// (it can always be set on a per-request basis or even using ajaxSetup)
  				// For same-domain requests, won't change header if already provided.
a1a3bc73   Luigi Serra   graphs updates
62
63
  				if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) {
  					headers[ "X-Requested-With" ] = "XMLHttpRequest";
74249687   Luigi Serra   Cross browser con...
64
65
66
67
68
69
70
71
72
73
74
  				}
  
  				// Set headers
  				for ( i in headers ) {
  					xhr.setRequestHeader( i, headers[ i ] );
  				}
  
  				// Callback
  				callback = function( type ) {
  					return function() {
  						if ( callback ) {
a1a3bc73   Luigi Serra   graphs updates
75
76
  							callback = errorCallback = xhr.onload =
  								xhr.onerror = xhr.onabort = xhr.onreadystatechange = null;
74249687   Luigi Serra   Cross browser con...
77
78
79
80
  
  							if ( type === "abort" ) {
  								xhr.abort();
  							} else if ( type === "error" ) {
a1a3bc73   Luigi Serra   graphs updates
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  
  								// Support: IE9
  								// On a manual native abort, IE9 throws
  								// errors on any property access that is not readyState
  								if ( typeof xhr.status !== "number" ) {
  									complete( 0, "error" );
  								} else {
  									complete(
  
  										// File: protocol always yields status 0; see #8605, #14207
  										xhr.status,
  										xhr.statusText
  									);
  								}
74249687   Luigi Serra   Cross browser con...
95
96
97
98
  							} else {
  								complete(
  									xhrSuccessStatus[ xhr.status ] || xhr.status,
  									xhr.statusText,
a1a3bc73   Luigi Serra   graphs updates
99
100
101
102
103
104
105
106
  
  									// Support: IE9 only
  									// IE9 has no XHR2 but throws on binary (trac-11426)
  									// For XHR2 non-text, let the caller handle it (gh-2498)
  									( xhr.responseType || "text" ) !== "text"  ||
  									typeof xhr.responseText !== "string" ?
  										{ binary: xhr.response } :
  										{ text: xhr.responseText },
74249687   Luigi Serra   Cross browser con...
107
108
109
110
111
112
113
114
115
  									xhr.getAllResponseHeaders()
  								);
  							}
  						}
  					};
  				};
  
  				// Listen to events
  				xhr.onload = callback();
a1a3bc73   Luigi Serra   graphs updates
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  				errorCallback = xhr.onerror = callback( "error" );
  
  				// Support: IE9
  				// Use onreadystatechange to replace onabort
  				// to handle uncaught aborts
  				if ( xhr.onabort !== undefined ) {
  					xhr.onabort = errorCallback;
  				} else {
  					xhr.onreadystatechange = function() {
  
  						// Check readyState before timeout as it changes
  						if ( xhr.readyState === 4 ) {
  
  							// Allow onerror to be called first,
  							// but that will not handle a native abort
  							// Also, save errorCallback to a variable
  							// as xhr.onerror cannot be accessed
  							window.setTimeout( function() {
  								if ( callback ) {
  									errorCallback();
  								}
  							} );
  						}
  					};
  				}
74249687   Luigi Serra   Cross browser con...
141
142
  
  				// Create the abort callback
a1a3bc73   Luigi Serra   graphs updates
143
  				callback = callback( "abort" );
74249687   Luigi Serra   Cross browser con...
144
145
  
  				try {
a1a3bc73   Luigi Serra   graphs updates
146
  
74249687   Luigi Serra   Cross browser con...
147
148
149
  					// Do send the request (this may raise an exception)
  					xhr.send( options.hasContent && options.data || null );
  				} catch ( e ) {
a1a3bc73   Luigi Serra   graphs updates
150
  
74249687   Luigi Serra   Cross browser con...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  					// #14683: Only rethrow if this hasn't been notified as an error yet
  					if ( callback ) {
  						throw e;
  					}
  				}
  			},
  
  			abort: function() {
  				if ( callback ) {
  					callback();
  				}
  			}
  		};
  	}
a1a3bc73   Luigi Serra   graphs updates
165
  } );
74249687   Luigi Serra   Cross browser con...
166
  
a1a3bc73   Luigi Serra   graphs updates
167
  } );