scratch – Blame information for rev 58

Subversion Repositories:
Rev:
Rev Author Line No. Line
58 office 1 define( [
2 "../core",
3 "../var/support",
4 "../ajax"
5 ], function( jQuery, support ) {
6  
7 "use strict";
8  
9 jQuery.ajaxSettings.xhr = function() {
10 try {
11 return new window.XMLHttpRequest();
12 } catch ( e ) {}
13 };
14  
15 var xhrSuccessStatus = {
16  
17 // File protocol always yields status code 0, assume 200
18 0: 200,
19  
20 // Support: IE <=9 only
21 // #1450: sometimes IE returns 1223 when it should be 204
22 1223: 204
23 },
24 xhrSupported = jQuery.ajaxSettings.xhr();
25  
26 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
27 support.ajax = xhrSupported = !!xhrSupported;
28  
29 jQuery.ajaxTransport( function( options ) {
30 var callback, errorCallback;
31  
32 // Cross domain only allowed if supported through XMLHttpRequest
33 if ( support.cors || xhrSupported && !options.crossDomain ) {
34 return {
35 send: function( headers, complete ) {
36 var i,
37 xhr = options.xhr();
38  
39 xhr.open(
40 options.type,
41 options.url,
42 options.async,
43 options.username,
44 options.password
45 );
46  
47 // Apply custom fields if provided
48 if ( options.xhrFields ) {
49 for ( i in options.xhrFields ) {
50 xhr[ i ] = options.xhrFields[ i ];
51 }
52 }
53  
54 // Override mime type if needed
55 if ( options.mimeType && xhr.overrideMimeType ) {
56 xhr.overrideMimeType( options.mimeType );
57 }
58  
59 // X-Requested-With header
60 // 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.
62 // (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.
64 if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) {
65 headers[ "X-Requested-With" ] = "XMLHttpRequest";
66 }
67  
68 // Set headers
69 for ( i in headers ) {
70 xhr.setRequestHeader( i, headers[ i ] );
71 }
72  
73 // Callback
74 callback = function( type ) {
75 return function() {
76 if ( callback ) {
77 callback = errorCallback = xhr.onload =
78 xhr.onerror = xhr.onabort = xhr.onreadystatechange = null;
79  
80 if ( type === "abort" ) {
81 xhr.abort();
82 } 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(
91  
92 // File: protocol always yields status 0; see #8605, #14207
93 xhr.status,
94 xhr.statusText
95 );
96 }
97 } else {
98 complete(
99 xhrSuccessStatus[ xhr.status ] || xhr.status,
100 xhr.statusText,
101  
102 // Support: IE <=9 only
103 // IE9 has no XHR2 but throws on binary (trac-11426)
104 // For XHR2 non-text, let the caller handle it (gh-2498)
105 ( xhr.responseType || "text" ) !== "text" ||
106 typeof xhr.responseText !== "string" ?
107 { binary: xhr.response } :
108 { text: xhr.responseText },
109 xhr.getAllResponseHeaders()
110 );
111 }
112 }
113 };
114 };
115  
116 // Listen to events
117 xhr.onload = callback();
118 errorCallback = 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  
144 // Create the abort callback
145 callback = callback( "abort" );
146  
147 try {
148  
149 // Do send the request (this may raise an exception)
150 xhr.send( options.hasContent && options.data || null );
151 } catch ( e ) {
152  
153 // #14683: Only rethrow if this hasn't been notified as an error yet
154 if ( callback ) {
155 throw e;
156 }
157 }
158 },
159  
160 abort: function() {
161 if ( callback ) {
162 callback();
163 }
164 }
165 };
166 }
167 } );
168  
169 } );