Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1.  
  2. function AJAX(config)
  3. {
  4.  
  5. if ( !(this instanceof AJAX) )
  6. {
  7. return new AJAX(config);
  8. }
  9.  
  10. this._xhr = new XMLHttpRequest();
  11. this._config = this._extendOptions(config);
  12.  
  13. this._assignEvents();
  14.  
  15. this._beforeSend();
  16. }
  17.  
  18. AJAX.prototype._extendOptions = function(config)
  19. {
  20. var defaultConfig = JSON.parse(JSON.stringify(this._defaultConfig));
  21.  
  22. for (var key in defaultConfig)
  23. {
  24. if (key in config)
  25. {
  26. continue;
  27. }
  28. config[key] = defaultConfig[key];
  29.  
  30. }
  31.  
  32. return config;
  33. }
  34.  
  35. AJAX.prototype._assignEvents = function()
  36. {
  37. this._xhr.addEventListener("readystatechange", this._handleResponse.bind(this), false);
  38. this._xhr.addEventListener("abort", this._handleError.bind(this), false);
  39. this._xhr.addEventListener("error", this._handleError.bind(this), false);
  40. this._xhr.addEventListener("timeout", this._handleError.bind(this), false);
  41. };
  42.  
  43. AJAX.prototype._assignUserHeaders = function()
  44. {
  45. if((Object.keys(this._config.headers)).length)
  46. {
  47. for (var key in this._config.headers)
  48. {
  49. this._xhr.setRequestHeader(key, this._config.headers[key]);
  50. }
  51. }
  52. };
  53.  
  54. AJAX.prototype._open = function()
  55. {
  56. this._xhr.open(
  57. this._config.type,
  58. this._config.url,
  59. this._config.options.async,
  60. this._config.options.username,
  61. this._config.options.password,
  62. );
  63. this._xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  64. this._xhr.timeout = this._config.options.timeout;
  65. };
  66.  
  67. AJAX.prototype._beforeSend = function()
  68. {
  69. var isData = Object.keys(this._config.data).length >0,
  70. data = null;
  71.  
  72. if(this._config.type.toUpperCase() === "POST" && isData)
  73. {
  74. data = this._serializeFormData(this._config.data);
  75. } else
  76. if(this._config.type.toUpperCase() === "GET" && isData)
  77. {
  78. this._config.url += "?" + this._serializeData(this._config.data);
  79. }
  80.  
  81. this._open();
  82. this._assignUserHeaders();
  83. this._send(data);
  84. };
  85.  
  86. AJAX.prototype._send = function(data)
  87. {
  88. this._xhr.send(data);
  89. };
  90.  
  91. AJAX.prototype._handleResponse = function(e)
  92. {
  93. if(this._xhr.readyState ===4 && this._xhr.status >= 200 && this._xhr.status < 400)
  94. {
  95. if(typeof this._config.success === "function")
  96. {
  97. this._config.success(this._xhr.response, this._xhr);
  98. }
  99. } else if (this._xhr.readyState === 4 && this._xhr.status >= 400)
  100. {
  101. this._handleError();
  102. }
  103. };
  104.  
  105.  
  106. AJAX.prototype._handleError = function(e)
  107. {
  108. if(typeof this._config.failure === "function")
  109. {
  110. this._config.failure(this._xhr);
  111. }
  112. };
  113.  
  114. AJAX.prototype._serializeData = function(data)
  115. {
  116. var serialized = "";
  117.  
  118. for (var key in data)
  119. {
  120. serialized += key + "=" + encodeURIComponent(data[key]) + "&";
  121. }
  122.  
  123. return serialized.slice(0, serialized.length - 1);
  124. };
  125.  
  126. AJAX.prototype._serializeFormData = function(data)
  127. {
  128. var serialized = new FormData();
  129.  
  130. for (var key in data)
  131. {
  132. serialized.append(key, data[key]);
  133. }
  134.  
  135. return serialized;
  136. }
  137.  
  138. AJAX.prototype._defaultConfig =
  139. {
  140. type: "GET",
  141. url: window.location.href,
  142. data: {},
  143. options:
  144. {
  145. async: true,
  146. timeout: 0,
  147. username: null,
  148. password: null,
  149. },
  150. headers: {}
  151. };
  152.  
  153. AJAX({
  154. type: "GET",
  155. url: "odbierz.php",
  156. data: {
  157. firstName: "Piotr ewe",
  158. lastName: "Kowalski Sas"
  159. },
  160. headers: {
  161. "X-My-Header": "123#asdf"
  162. },
  163. success: function(response, xhr) {
  164. console.log("Udało się! Status: " + xhr.status);
  165. console.log(response);
  166. },
  167. failure: function(xhr) {
  168. console.log("Wystąpił błąd. Status: " + xhr.status);
  169. }
  170. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement