scratch – Blame information for rev 125

Subversion Repositories:
Rev:
Rev Author Line No. Line
125 office 1 define([
58 office 2 "../core",
3 "../var/support",
4 "../ajax"
5 ], function( jQuery, support ) {
6  
7 jQuery.ajaxSettings.xhr = function() {
8 try {
125 office 9 return new XMLHttpRequest();
10 } catch( e ) {}
58 office 11 };
12  
125 office 13 var xhrId = 0,
14 xhrCallbacks = {},
15 xhrSuccessStatus = {
16 // file protocol always yields status code 0, assume 200
58 office 17 0: 200,
125 office 18 // Support: IE9
58 office 19 // #1450: sometimes IE returns 1223 when it should be 204
20 1223: 204
21 },
22 xhrSupported = jQuery.ajaxSettings.xhr();
23  
125 office 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 }
34  
58 office 35 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
36 support.ajax = xhrSupported = !!xhrSupported;
37  
125 office 38 jQuery.ajaxTransport(function( options ) {
39 var callback;
58 office 40  
41 // Cross domain only allowed if supported through XMLHttpRequest
42 if ( support.cors || xhrSupported && !options.crossDomain ) {
43 return {
44 send: function( headers, complete ) {
45 var i,
125 office 46 xhr = options.xhr(),
47 id = ++xhrId;
58 office 48  
125 office 49 xhr.open( options.type, options.url, options.async, options.username, options.password );
58 office 50  
51 // Apply custom fields if provided
52 if ( options.xhrFields ) {
53 for ( i in options.xhrFields ) {
54 xhr[ i ] = options.xhrFields[ i ];
55 }
56 }
57  
58 // Override mime type if needed
59 if ( options.mimeType && xhr.overrideMimeType ) {
60 xhr.overrideMimeType( options.mimeType );
61 }
62  
63 // X-Requested-With header
64 // For cross-domain requests, seeing as conditions for a preflight are
65 // akin to a jigsaw puzzle, we simply never set it to be sure.
66 // (it can always be set on a per-request basis or even using ajaxSetup)
67 // For same-domain requests, won't change header if already provided.
125 office 68 if ( !options.crossDomain && !headers["X-Requested-With"] ) {
69 headers["X-Requested-With"] = "XMLHttpRequest";
58 office 70 }
71  
72 // Set headers
73 for ( i in headers ) {
74 xhr.setRequestHeader( i, headers[ i ] );
75 }
76  
77 // Callback
78 callback = function( type ) {
79 return function() {
80 if ( callback ) {
125 office 81 delete xhrCallbacks[ id ];
82 callback = xhr.onload = xhr.onerror = null;
58 office 83  
84 if ( type === "abort" ) {
85 xhr.abort();
86 } else if ( type === "error" ) {
125 office 87 complete(
88 // file: protocol always yields status 0; see #8605, #14207
89 xhr.status,
90 xhr.statusText
91 );
58 office 92 } else {
93 complete(
94 xhrSuccessStatus[ xhr.status ] || xhr.status,
95 xhr.statusText,
125 office 96 // Support: IE9
97 // Accessing binary-data responseText throws an exception
98 // (#11426)
99 typeof xhr.responseText === "string" ? {
100 text: xhr.responseText
101 } : undefined,
58 office 102 xhr.getAllResponseHeaders()
103 );
104 }
105 }
106 };
107 };
108  
109 // Listen to events
110 xhr.onload = callback();
125 office 111 xhr.onerror = callback("error");
58 office 112  
113 // Create the abort callback
125 office 114 callback = xhrCallbacks[ id ] = callback("abort");
58 office 115  
116 try {
117 // Do send the request (this may raise an exception)
118 xhr.send( options.hasContent && options.data || null );
119 } catch ( e ) {
120 // #14683: Only rethrow if this hasn't been notified as an error yet
121 if ( callback ) {
122 throw e;
123 }
124 }
125 },
126  
127 abort: function() {
128 if ( callback ) {
129 callback();
130 }
131 }
132 };
133 }
125 office 134 });
58 office 135  
125 office 136 });