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