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