diff options
| author | Jonathan Barronville | 2014-06-07 19:13:28 -0400 | 
|---|---|---|
| committer | Jonathan Barronville | 2014-06-07 19:13:28 -0400 | 
| commit | b56e5c671ce89f1c8ddc67a4ac8d2f59de04ea85 (patch) | |
| tree | 80ce4d67ff46505dc1ec77b72daf7bed59abf8b4 /bower_components/jquery/src/ajax/xhr.js | |
| parent | 67ad58c27c4a2704532246d044e1ecbae2a11022 (diff) | |
| download | sellevate-b56e5c671ce89f1c8ddc67a4ac8d2f59de04ea85.tar.bz2 | |
Yo.
Diffstat (limited to 'bower_components/jquery/src/ajax/xhr.js')
| -rw-r--r-- | bower_components/jquery/src/ajax/xhr.js | 135 | 
1 files changed, 135 insertions, 0 deletions
| diff --git a/bower_components/jquery/src/ajax/xhr.js b/bower_components/jquery/src/ajax/xhr.js new file mode 100644 index 0000000..bdeeee3 --- /dev/null +++ b/bower_components/jquery/src/ajax/xhr.js @@ -0,0 +1,135 @@ +define([ +	"../core", +	"../var/support", +	"../ajax" +], function( jQuery, support ) { + +jQuery.ajaxSettings.xhr = function() { +	try { +		return new XMLHttpRequest(); +	} catch( e ) {} +}; + +var xhrId = 0, +	xhrCallbacks = {}, +	xhrSuccessStatus = { +		// file protocol always yields status code 0, assume 200 +		0: 200, +		// Support: IE9 +		// #1450: sometimes IE returns 1223 when it should be 204 +		1223: 204 +	}, +	xhrSupported = jQuery.ajaxSettings.xhr(); + +// Support: IE9 +// Open requests must be manually aborted on unload (#5280) +if ( window.ActiveXObject ) { +	jQuery( window ).on( "unload", function() { +		for ( var key in xhrCallbacks ) { +			xhrCallbacks[ key ](); +		} +	}); +} + +support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported ); +support.ajax = xhrSupported = !!xhrSupported; + +jQuery.ajaxTransport(function( options ) { +	var callback; + +	// Cross domain only allowed if supported through XMLHttpRequest +	if ( support.cors || xhrSupported && !options.crossDomain ) { +		return { +			send: function( headers, complete ) { +				var i, +					xhr = options.xhr(), +					id = ++xhrId; + +				xhr.open( options.type, options.url, options.async, options.username, options.password ); + +				// 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. +				if ( !options.crossDomain && !headers["X-Requested-With"] ) { +					headers["X-Requested-With"] = "XMLHttpRequest"; +				} + +				// Set headers +				for ( i in headers ) { +					xhr.setRequestHeader( i, headers[ i ] ); +				} + +				// Callback +				callback = function( type ) { +					return function() { +						if ( callback ) { +							delete xhrCallbacks[ id ]; +							callback = xhr.onload = xhr.onerror = null; + +							if ( type === "abort" ) { +								xhr.abort(); +							} else if ( type === "error" ) { +								complete( +									// file: protocol always yields status 0; see #8605, #14207 +									xhr.status, +									xhr.statusText +								); +							} else { +								complete( +									xhrSuccessStatus[ xhr.status ] || xhr.status, +									xhr.statusText, +									// Support: IE9 +									// Accessing binary-data responseText throws an exception +									// (#11426) +									typeof xhr.responseText === "string" ? { +										text: xhr.responseText +									} : undefined, +									xhr.getAllResponseHeaders() +								); +							} +						} +					}; +				}; + +				// Listen to events +				xhr.onload = callback(); +				xhr.onerror = callback("error"); + +				// Create the abort callback +				callback = xhrCallbacks[ id ] = callback("abort"); + +				try { +					// Do send the request (this may raise an exception) +					xhr.send( options.hasContent && options.data || null ); +				} catch ( e ) { +					// #14683: Only rethrow if this hasn't been notified as an error yet +					if ( callback ) { +						throw e; +					} +				} +			}, + +			abort: function() { +				if ( callback ) { +					callback(); +				} +			} +		}; +	} +}); + +}); | 
