]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/space-pen/node_modules/jquery/src/ajax/xhr.js
c2b43c92115a5b950229d3e6743008dc7fba04f8
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / space-pen / node_modules / 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 XMLHttpRequest();
10 } catch( e ) {}
11 };
12
13 var xhrId = 0,
14 xhrCallbacks = {},
15 xhrSuccessStatus = {
16 // file protocol always yields status code 0, assume 200
17 0: 200,
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: 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
35 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
36 support.ajax = xhrSupported = !!xhrSupported;
37
38 jQuery.ajaxTransport(function( options ) {
39 var callback;
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,
46 xhr = options.xhr(),
47 id = ++xhrId;
48
49 xhr.open( options.type, options.url, options.async, options.username, options.password );
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.
68 if ( !options.crossDomain && !headers["X-Requested-With"] ) {
69 headers["X-Requested-With"] = "XMLHttpRequest";
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 ) {
81 delete xhrCallbacks[ id ];
82 callback = xhr.onload = xhr.onerror = null;
83
84 if ( type === "abort" ) {
85 xhr.abort();
86 } else if ( type === "error" ) {
87 complete(
88 // file: protocol always yields status 0; see #8605, #14207
89 xhr.status,
90 xhr.statusText
91 );
92 } else {
93 complete(
94 xhrSuccessStatus[ xhr.status ] || xhr.status,
95 xhr.statusText,
96 // Support: IE9
97 // Accessing binary-data responseText throws an exception
98 // (#11426)
99 typeof xhr.responseText === "string" ? {
100 text: xhr.responseText
101 } : undefined,
102 xhr.getAllResponseHeaders()
103 );
104 }
105 }
106 };
107 };
108
109 // Listen to events
110 xhr.onload = callback();
111 xhr.onerror = callback("error");
112
113 // Create the abort callback
114 callback = xhrCallbacks[ id ] = callback("abort");
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 }
134 });
135
136 });