PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/js/jquery/src/jquery/ajax/xhr.js

https://gitlab.com/qbarbosa/klindev
JavaScript | 196 lines | 111 code | 30 blank | 55 comment | 35 complexity | 6640ee85cecf8fddfb2cf74998f8acb1 MD5 | raw file
  1. define([
  2. "../core",
  3. "../var/support",
  4. "../ajax"
  5. ], function( jQuery, support ) {
  6. // Create the request object
  7. // (This is still attached to ajaxSettings for backward compatibility)
  8. jQuery.ajaxSettings.xhr = window.ActiveXObject !== undefined ?
  9. // Support: IE6+
  10. function() {
  11. // XHR cannot access local files, always use ActiveX for that case
  12. return !this.isLocal &&
  13. // Support: IE7-8
  14. // oldIE XHR does not support non-RFC2616 methods (#13240)
  15. // See http://msdn.microsoft.com/en-us/library/ie/ms536648(v=vs.85).aspx
  16. // and http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9
  17. // Although this check for six methods instead of eight
  18. // since IE also does not support "trace" and "connect"
  19. /^(get|post|head|put|delete|options)$/i.test( this.type ) &&
  20. createStandardXHR() || createActiveXHR();
  21. } :
  22. // For all other browsers, use the standard XMLHttpRequest object
  23. createStandardXHR;
  24. var xhrId = 0,
  25. xhrCallbacks = {},
  26. xhrSupported = jQuery.ajaxSettings.xhr();
  27. // Support: IE<10
  28. // Open requests must be manually aborted on unload (#5280)
  29. if ( window.ActiveXObject ) {
  30. jQuery( window ).on( "unload", function() {
  31. for ( var key in xhrCallbacks ) {
  32. xhrCallbacks[ key ]( undefined, true );
  33. }
  34. });
  35. }
  36. // Determine support properties
  37. support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
  38. xhrSupported = support.ajax = !!xhrSupported;
  39. // Create transport if the browser can provide an xhr
  40. if ( xhrSupported ) {
  41. jQuery.ajaxTransport(function( options ) {
  42. // Cross domain only allowed if supported through XMLHttpRequest
  43. if ( !options.crossDomain || support.cors ) {
  44. var callback;
  45. return {
  46. send: function( headers, complete ) {
  47. var i,
  48. xhr = options.xhr(),
  49. id = ++xhrId;
  50. // Open the socket
  51. xhr.open( options.type, options.url, options.async, options.username, options.password );
  52. // Apply custom fields if provided
  53. if ( options.xhrFields ) {
  54. for ( i in options.xhrFields ) {
  55. xhr[ i ] = options.xhrFields[ i ];
  56. }
  57. }
  58. // Override mime type if needed
  59. if ( options.mimeType && xhr.overrideMimeType ) {
  60. xhr.overrideMimeType( options.mimeType );
  61. }
  62. // X-Requested-With header
  63. // For cross-domain requests, seeing as conditions for a preflight are
  64. // akin to a jigsaw puzzle, we simply never set it to be sure.
  65. // (it can always be set on a per-request basis or even using ajaxSetup)
  66. // For same-domain requests, won't change header if already provided.
  67. if ( !options.crossDomain && !headers["X-Requested-With"] ) {
  68. headers["X-Requested-With"] = "XMLHttpRequest";
  69. }
  70. // Set headers
  71. for ( i in headers ) {
  72. // Support: IE<9
  73. // IE's ActiveXObject throws a 'Type Mismatch' exception when setting
  74. // request header to a null-value.
  75. //
  76. // To keep consistent with other XHR implementations, cast the value
  77. // to string and ignore `undefined`.
  78. if ( headers[ i ] !== undefined ) {
  79. xhr.setRequestHeader( i, headers[ i ] + "" );
  80. }
  81. }
  82. // Do send the request
  83. // This may raise an exception which is actually
  84. // handled in jQuery.ajax (so no try/catch here)
  85. xhr.send( ( options.hasContent && options.data ) || null );
  86. // Listener
  87. callback = function( _, isAbort ) {
  88. var status, statusText, responses;
  89. // Was never called and is aborted or complete
  90. if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
  91. // Clean up
  92. delete xhrCallbacks[ id ];
  93. callback = undefined;
  94. xhr.onreadystatechange = jQuery.noop;
  95. // Abort manually if needed
  96. if ( isAbort ) {
  97. if ( xhr.readyState !== 4 ) {
  98. xhr.abort();
  99. }
  100. } else {
  101. responses = {};
  102. status = xhr.status;
  103. // Support: IE<10
  104. // Accessing binary-data responseText throws an exception
  105. // (#11426)
  106. if ( typeof xhr.responseText === "string" ) {
  107. responses.text = xhr.responseText;
  108. }
  109. // Firefox throws an exception when accessing
  110. // statusText for faulty cross-domain requests
  111. try {
  112. statusText = xhr.statusText;
  113. } catch( e ) {
  114. // We normalize with Webkit giving an empty statusText
  115. statusText = "";
  116. }
  117. // Filter status for non standard behaviors
  118. // If the request is local and we have data: assume a success
  119. // (success with no data won't get notified, that's the best we
  120. // can do given current implementations)
  121. if ( !status && options.isLocal && !options.crossDomain ) {
  122. status = responses.text ? 200 : 404;
  123. // IE - #1450: sometimes returns 1223 when it should be 204
  124. } else if ( status === 1223 ) {
  125. status = 204;
  126. }
  127. }
  128. }
  129. // Call complete if needed
  130. if ( responses ) {
  131. complete( status, statusText, responses, xhr.getAllResponseHeaders() );
  132. }
  133. };
  134. if ( !options.async ) {
  135. // if we're in sync mode we fire the callback
  136. callback();
  137. } else if ( xhr.readyState === 4 ) {
  138. // (IE6 & IE7) if it's in cache and has been
  139. // retrieved directly we need to fire the callback
  140. setTimeout( callback );
  141. } else {
  142. // Add to the list of active xhr callbacks
  143. xhr.onreadystatechange = xhrCallbacks[ id ] = callback;
  144. }
  145. },
  146. abort: function() {
  147. if ( callback ) {
  148. callback( undefined, true );
  149. }
  150. }
  151. };
  152. }
  153. });
  154. }
  155. // Functions to create xhrs
  156. function createStandardXHR() {
  157. try {
  158. return new window.XMLHttpRequest();
  159. } catch( e ) {}
  160. }
  161. function createActiveXHR() {
  162. try {
  163. return new window.ActiveXObject( "Microsoft.XMLHTTP" );
  164. } catch( e ) {}
  165. }
  166. });