Advertisement
Guest User

Untitled

a guest
May 4th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. function xhr(options, cb) {
  2. if(!options) {
  3. throw '[ajax] missing required options';
  4. }
  5.  
  6. var url = options.url ? options.url : window.location.href;
  7. var method = options.method ? options.method.toUpperCase() : 'GET';
  8. var data = options.data ? JSON.stringify(options.data) : null;
  9. var contentType = (typeof options.contentType !== 'undefined') ? options.contentType : null;
  10. var headers = (options.headers && typeof options.headers === 'object') ? options.headers : {};
  11. var username = options.username ? options.username : null;
  12. var password = options.password ? options.password : null;
  13. var timeout = options.timeout ? options.timeout : 0;
  14. var withCredentials = (typeof options.withCredentials !== 'undefined') ? options.withCredentials : false;
  15. var async = (typeof options.async !== 'undefined') ? options.async : true;
  16. var onError = (options.onError && typeof options.onError === 'function') ? options.onError : null;
  17. var onSuccess = (options.onSuccess && typeof options.onSuccess === 'function') ? options.onSuccess : null;
  18. var onTimeout = (options.onTimeout && typeof options.onTimeout === 'function') ? options.onTimeout : null;
  19.  
  20. var _xhr = new XMLHttpRequest();
  21. _xhr.open(method, url, async, username, password);
  22.  
  23. // can only set a timeout on asynchronous requests
  24. if(async) {
  25. _xhr.timeout = timeout;
  26. }
  27.  
  28. if(withCredentials) {
  29. _xhr.withCredentials = withCredentials;
  30. }
  31.  
  32. if(contentType) {
  33. _xhr.setRequestHeader('Content-Type', contentType);
  34. } else if(contentType !== false && method === 'POST') {
  35. _xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  36. }
  37.  
  38. for (var key in headers) {
  39. _xhr.setRequestHeader(key, headers[key]);
  40. }
  41.  
  42. _xhr.onreadystatechange = function() {
  43. if (this.readyState === 4) {
  44. if (this.status >= 200 && this.status < 400) {
  45. if(onSuccess) {
  46. onSuccess(this.responseText, this.status, this);
  47. }
  48. } else {
  49. if(onError) {
  50. onError(this, this.status);
  51. }
  52. }
  53. }
  54. };
  55.  
  56. if(onTimeout) {
  57. _xhr.ontimeout = onTimeout;
  58. }
  59.  
  60. _xhr.send(data);
  61. _xhr = null;
  62. }
  63.  
  64. xhr({
  65. url: "http://foo.bar/customers"
  66. }
  67.  
  68. xhr({
  69. url: "http://foo.bar/customers",
  70. method: "POST",
  71. data: 'foo=bar'
  72. }
  73.  
  74. xhr({
  75. url: "http://foo.bar/customers",
  76. method: "POST",
  77. contentType: "application/json; charset=UTF-8"
  78. data: { foo: "bar" },
  79. onSuccess: function(response, statusCode, xhr) {
  80. }
  81. onError: function(xhr, statusCode) {
  82. },
  83. onTimeout: function(event) {
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement