scratch – Diff between revs 58 and 125

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 58 Rev 125
Line 1... Line 1...
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 ) {
Line 6... Line -...
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 ) {}
Line -... Line 11...
-   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
Line -... Line 21...
-   21 },
-   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 }
23 }, 32 });
24 xhrSupported = jQuery.ajaxSettings.xhr(); 33 }
Line 25... Line 34...
25   34  
26 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported ); 35 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
Line 27... Line 36...
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
-   42 if ( support.cors || xhrSupported && !options.crossDomain ) {
Line 33... Line -...
33 if ( support.cors || xhrSupported && !options.crossDomain ) { -  
34 return { -  
35 send: function( headers, complete ) { -  
36 var i, -  
37 xhr = options.xhr(); 43 return {
38   -  
39 xhr.open( -  
Line 40... Line 44...
40 options.type, 44 send: function( headers, complete ) {
41 options.url, 45 var i,
42 options.async, 46 xhr = options.xhr(),
43 options.username, 47 id = ++xhrId;
Line 59... Line 63...
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 }
Line 67... Line 71...
67   71  
68 // Set headers 72 // Set headers
69 for ( i in headers ) { 73 for ( i in headers ) {
Line 72... Line 76...
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 ];
Line 78... Line 82...
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(); -  
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" ); 85 xhr.abort();
89 } else { -  
90 complete( 86 } else if ( type === "error" ) {
91   87 complete(
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 -  
95 ); 90 xhr.statusText
96 } 91 );
97 } else { 92 } else {
98 complete( 93 complete(
99 xhrSuccessStatus[ xhr.status ] || xhr.status, -  
100 xhr.statusText, 94 xhrSuccessStatus[ xhr.status ] || xhr.status,
101   95 xhr.statusText,
102 // Support: IE <=9 only -  
103 // IE9 has no XHR2 but throws on binary (trac-11426) 96 // Support: IE9
104 // For XHR2 non-text, let the caller handle it (gh-2498) 97 // Accessing binary-data responseText throws an exception
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 }
Line 113... Line 106...
113 }; 106 };
114 }; 107 };
115   108  
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 } ); -  
Line 140... Line 109...
140 } 109 // Listen to events
141 }; 110 xhr.onload = callback();
Line 142... Line 111...
142 } 111 xhr.onerror = callback("error");
143   -  
144 // Create the abort callback 112  
145 callback = callback( "abort" ); 113 // Create the abort callback
146   114 callback = xhrCallbacks[ id ] = callback("abort");
147 try { -  
148   115  
149 // Do send the request (this may raise an exception) 116 try {
150 xhr.send( options.hasContent && options.data || null ); 117 // Do send the request (this may raise an exception)
151 } catch ( e ) { 118 xhr.send( options.hasContent && options.data || null );
152   119 } catch ( e ) {
Line 162... Line 129...
162 callback(); 129 callback();
163 } 130 }
164 } 131 }
165 }; 132 };
166 } 133 }
167 } ); 134 });
Line 168... Line 135...
168   135