Blame view

bower_components/jquery/src/ajax/xhr.js 3.41 KB
c5169e0e   Renato De Donato   a new hope
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 {
c5169e0e   Renato De Donato   a new hope
9
10
  		return new XMLHttpRequest();
  	} catch( e ) {}
74249687   Luigi Serra   Cross browser con...
11
12
  };
  
c5169e0e   Renato De Donato   a new hope
13
14
15
16
  var xhrId = 0,
  	xhrCallbacks = {},
  	xhrSuccessStatus = {
  		// file protocol always yields status code 0, assume 200
74249687   Luigi Serra   Cross browser con...
17
  		0: 200,
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();
  
c5169e0e   Renato De Donato   a new hope
24
25
26
27
28
29
30
31
32
33
34
  // Support: IE9
  // Open requests must be manually aborted on unload (#5280)
  // See https://support.microsoft.com/kb/2856746 for more info
  if ( window.attachEvent ) {
  	window.attachEvent( "onunload", function() {
  		for ( var key in xhrCallbacks ) {
  			xhrCallbacks[ key ]();
  		}
  	});
  }
  
74249687   Luigi Serra   Cross browser con...
35
36
37
  support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
  support.ajax = xhrSupported = !!xhrSupported;
  
c5169e0e   Renato De Donato   a new hope
38
39
  jQuery.ajaxTransport(function( options ) {
  	var callback;
74249687   Luigi Serra   Cross browser con...
40
41
42
43
44
45
  
  	// Cross domain only allowed if supported through XMLHttpRequest
  	if ( support.cors || xhrSupported && !options.crossDomain ) {
  		return {
  			send: function( headers, complete ) {
  				var i,
c5169e0e   Renato De Donato   a new hope
46
47
  					xhr = options.xhr(),
  					id = ++xhrId;
74249687   Luigi Serra   Cross browser con...
48
  
c5169e0e   Renato De Donato   a new hope
49
  				xhr.open( options.type, options.url, options.async, options.username, options.password );
74249687   Luigi Serra   Cross browser con...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  
  				// 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.
c5169e0e   Renato De Donato   a new hope
68
69
  				if ( !options.crossDomain && !headers["X-Requested-With"] ) {
  					headers["X-Requested-With"] = "XMLHttpRequest";
74249687   Luigi Serra   Cross browser con...
70
71
72
73
74
75
76
77
78
79
80
  				}
  
  				// Set headers
  				for ( i in headers ) {
  					xhr.setRequestHeader( i, headers[ i ] );
  				}
  
  				// Callback
  				callback = function( type ) {
  					return function() {
  						if ( callback ) {
c5169e0e   Renato De Donato   a new hope
81
82
  							delete xhrCallbacks[ id ];
  							callback = xhr.onload = xhr.onerror = null;
74249687   Luigi Serra   Cross browser con...
83
84
85
86
  
  							if ( type === "abort" ) {
  								xhr.abort();
  							} else if ( type === "error" ) {
c5169e0e   Renato De Donato   a new hope
87
88
89
90
91
  								complete(
  									// file: protocol always yields status 0; see #8605, #14207
  									xhr.status,
  									xhr.statusText
  								);
74249687   Luigi Serra   Cross browser con...
92
93
94
95
  							} else {
  								complete(
  									xhrSuccessStatus[ xhr.status ] || xhr.status,
  									xhr.statusText,
c5169e0e   Renato De Donato   a new hope
96
97
98
99
100
101
  									// Support: IE9
  									// Accessing binary-data responseText throws an exception
  									// (#11426)
  									typeof xhr.responseText === "string" ? {
  										text: xhr.responseText
  									} : undefined,
74249687   Luigi Serra   Cross browser con...
102
103
104
105
106
107
108
109
110
  									xhr.getAllResponseHeaders()
  								);
  							}
  						}
  					};
  				};
  
  				// Listen to events
  				xhr.onload = callback();
c5169e0e   Renato De Donato   a new hope
111
  				xhr.onerror = callback("error");
74249687   Luigi Serra   Cross browser con...
112
113
  
  				// Create the abort callback
c5169e0e   Renato De Donato   a new hope
114
  				callback = xhrCallbacks[ id ] = callback("abort");
74249687   Luigi Serra   Cross browser con...
115
116
  
  				try {
74249687   Luigi Serra   Cross browser con...
117
118
119
  					// Do send the request (this may raise an exception)
  					xhr.send( options.hasContent && options.data || null );
  				} catch ( e ) {
74249687   Luigi Serra   Cross browser con...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  					// #14683: Only rethrow if this hasn't been notified as an error yet
  					if ( callback ) {
  						throw e;
  					}
  				}
  			},
  
  			abort: function() {
  				if ( callback ) {
  					callback();
  				}
  			}
  		};
  	}
c5169e0e   Renato De Donato   a new hope
134
  });
74249687   Luigi Serra   Cross browser con...
135
  
c5169e0e   Renato De Donato   a new hope
136
  });