xonecas.io
286
Sf

simple-flask-coffee-sass-boilerplate / files

simple-flask-coffee-sass-boilerplate / bower_components / jquery / src / ajax / xhr.js

3.3 KB
1define([
2	"../core",
3	"../var/support",
4	"../ajax"
5], function( jQuery, support ) {
6
7jQuery.ajaxSettings.xhr = function() {
8	try {
9		return new XMLHttpRequest();
10	} catch( e ) {}
11};
12
13var xhrId = 0,
14	xhrCallbacks = {},
15	xhrSuccessStatus = {
16		// file protocol always yields status code 0, assume 200
17		0: 200,
18		// Support: IE9
19		// #1450: sometimes IE returns 1223 when it should be 204
20		1223: 204
21	},
22	xhrSupported = jQuery.ajaxSettings.xhr();
23
24// Support: IE9
25// Open requests must be manually aborted on unload (#5280)
26if ( window.ActiveXObject ) {
27	jQuery( window ).on( "unload", function() {
28		for ( var key in xhrCallbacks ) {
29			xhrCallbacks[ key ]();
30		}
31	});
32}
33
34support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
35support.ajax = xhrSupported = !!xhrSupported;
36
37jQuery.ajaxTransport(function( options ) {
38	var callback;
39
40	// Cross domain only allowed if supported through XMLHttpRequest
41	if ( support.cors || xhrSupported && !options.crossDomain ) {
42		return {
43			send: function( headers, complete ) {
44				var i,
45					xhr = options.xhr(),
46					id = ++xhrId;
47
48				xhr.open( options.type, options.url, options.async, options.username, options.password );
49
50				// Apply custom fields if provided
51				if ( options.xhrFields ) {
52					for ( i in options.xhrFields ) {
53						xhr[ i ] = options.xhrFields[ i ];
54					}
55				}
56
57				// Override mime type if needed
58				if ( options.mimeType && xhr.overrideMimeType ) {
59					xhr.overrideMimeType( options.mimeType );
60				}
61
62				// X-Requested-With header
63				// For cross-domain requests, seeing as conditions for a preflight are
64				// akin to a jigsaw puzzle, we simply never set it to be sure.
65				// (it can always be set on a per-request basis or even using ajaxSetup)
66				// For same-domain requests, won't change header if already provided.
67				if ( !options.crossDomain && !headers["X-Requested-With"] ) {
68					headers["X-Requested-With"] = "XMLHttpRequest";
69				}
70
71				// Set headers
72				for ( i in headers ) {
73					xhr.setRequestHeader( i, headers[ i ] );
74				}
75
76				// Callback
77				callback = function( type ) {
78					return function() {
79						if ( callback ) {
80							delete xhrCallbacks[ id ];
81							callback = xhr.onload = xhr.onerror = null;
82
83							if ( type === "abort" ) {
84								xhr.abort();
85							} else if ( type === "error" ) {
86								complete(
87									// file: protocol always yields status 0; see #8605, #14207
88									xhr.status,
89									xhr.statusText
90								);
91							} else {
92								complete(
93									xhrSuccessStatus[ xhr.status ] || xhr.status,
94									xhr.statusText,
95									// Support: IE9
96									// Accessing binary-data responseText throws an exception
97									// (#11426)
98									typeof xhr.responseText === "string" ? {
99										text: xhr.responseText
100									} : undefined,
101									xhr.getAllResponseHeaders()
102								);
103							}
104						}
105					};
106				};
107
108				// Listen to events
109				xhr.onload = callback();
110				xhr.onerror = callback("error");
111
112				// Create the abort callback
113				callback = xhrCallbacks[ id ] = callback("abort");
114
115				try {
116					// Do send the request (this may raise an exception)
117					xhr.send( options.hasContent && options.data || null );
118				} catch ( e ) {
119					// #14683: Only rethrow if this hasn't been notified as an error yet
120					if ( callback ) {
121						throw e;
122					}
123				}
124			},
125
126			abort: function() {
127				if ( callback ) {
128					callback();
129				}
130			}
131		};
132	}
133});
134
135});