API Docs for: 1.5.0
Show:

File: src/connections/MicrosoftXmlHttpRequestConnection.js

  1. /* global define */
  2. /* global ActiveXObject */
  3. define(["structures/Response", "structures/CAPIError"], function (Response, CAPIError) {
  4. "use strict";
  5.  
  6. /**
  7. * Creates an instance of MicrosoftXmlHttpRequestConnection object
  8. * This connection class handles low-level implementation of XHR connection for Microsoft browsers
  9. *
  10. * @class MicrosoftXmlHttpRequestConnection
  11. * @constructor
  12. * @deprecated since 1.2
  13. */
  14. var MicrosoftXmlHttpRequestConnection = function () {
  15. console.warn('[DEPRECATED] MicrosoftXmlHttpRequestConnection is deprecated and will be removed in eZ JS REST client 2.0');
  16. this._xhr = new ActiveXObject("Microsoft.XMLHTTP");
  17. };
  18.  
  19. /**
  20. * Basic request implemented via XHR technique
  21. *
  22. * @method execute
  23. * @param request {Request} structure containing all needed params and data
  24. * @param callback {Function} function, which will be executed on request success
  25. */
  26. MicrosoftXmlHttpRequestConnection.prototype.execute = function (request, callback) {
  27. var XHR = this._xhr,
  28. headerType;
  29.  
  30. // Create the state change handler:
  31. XHR.onreadystatechange = function () {
  32. var response;
  33.  
  34. if (XHR.readyState != 4) {return;} // Not ready yet
  35.  
  36. response = new Response({
  37. status: XHR.status,
  38. headers: XHR.getAllResponseHeaders(),
  39. body: XHR.responseText,
  40. xhr: XHR,
  41. });
  42.  
  43. if (XHR.status >= 400 || !XHR.status) {
  44. callback(
  45. new CAPIError("Connection error : " + XHR.status + ".", {request: request}),
  46. response
  47. );
  48. return;
  49. }
  50. callback(false, response);
  51. };
  52.  
  53. if (request.httpBasicAuth) {
  54. XHR.open(request.method, request.url, true, request.login, request.password);
  55. } else {
  56. XHR.open(request.method, request.url, true);
  57. }
  58.  
  59. for (headerType in request.headers) {
  60. if (request.headers.hasOwnProperty(headerType)) {
  61. XHR.setRequestHeader(
  62. headerType,
  63. request.headers[headerType]
  64. );
  65. }
  66. }
  67. XHR.send(request.body);
  68. };
  69.  
  70. /**
  71. * Connection checks itself for compatibility with running environment
  72. *
  73. * @method isCompatible
  74. * @static
  75. * @return {Boolean} whether the connection is compatible with current environment
  76. */
  77. MicrosoftXmlHttpRequestConnection.isCompatible = function () {
  78. return !!window.ActiveXObject;
  79. };
  80.  
  81. return MicrosoftXmlHttpRequestConnection;
  82. });
  83.