ajax.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. function serialize(obj) {
  2. return Object.keys(obj).map(function(k) {return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]);}).join('&');
  3. }
  4. function ajaxPost(url, data, headers) {
  5. var promise = {"success": function() {},
  6. "failure": function() {}};
  7. var xhttp = new XMLHttpRequest();
  8. xhttp.open("POST", url, true);
  9. if(typeof headers != typeof undefined) {
  10. for (var key in headers) {
  11. xhttp.setRequestHeader(key, headers[key]);
  12. }
  13. }
  14. xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  15. xhttp.onreadystatechange = function() {
  16. if (this.readyState == 4) {
  17. if(this.status == 200) {
  18. promise.success(JSON.parse(this.responseText));
  19. } else {
  20. var errorData = {};
  21. errorData.message = this.status + " " + this.statusText;
  22. errorData.response = this.responseText;
  23. promise.failure(errorData);
  24. }
  25. }
  26. };
  27. var dataString = serialize(data);
  28. xhttp.send(dataString);
  29. return promise;
  30. }
  31. function ajaxPut(url, binaryData, headers) {
  32. var promise = {"success": function() {},
  33. "failure": function() {},
  34. "finally": function() {}};
  35. var xhttp = new XMLHttpRequest();
  36. xhttp.open("PUT", url, true);
  37. if(typeof headers != typeof undefined) {
  38. for (var key in headers) {
  39. xhttp.setRequestHeader(key, headers[key]);
  40. }
  41. }
  42. contentType = "text/plain";
  43. if('content-type' in headers) {
  44. contentType = headers['content-type'];
  45. }
  46. xhttp.overrideMimeType(contentType + '; charset=x-user-defined-binary');
  47. xhttp.onreadystatechange = function() {
  48. if (this.readyState == 4) {
  49. if(this.status == 200) {
  50. var successData = {};
  51. successData.message = this.status + " " + this.statusText;
  52. successData.response = this.responseText;
  53. successData.url = this.responseURL;
  54. promise.success(successData);
  55. } else {
  56. var errorData = {};
  57. errorData.message = this.status + " " + this.statusText;
  58. errorData.response = this.responseText;
  59. promise.failure(errorData);
  60. }
  61. promise.finally();
  62. }
  63. };
  64. xhttp.send(binaryData);
  65. return promise;
  66. }
  67. function ajaxGet(url, headers) {
  68. var promise = {"success": function() {},
  69. "failure": function() {}};
  70. var xhttp = new XMLHttpRequest();
  71. xhttp.open("GET", url, true);
  72. if(typeof headers != typeof undefined) {
  73. for (var key in headers) {
  74. xhttp.setRequestHeader(key, headers[key]);
  75. }
  76. }
  77. //xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  78. xhttp.onreadystatechange = function() {
  79. if (this.readyState == 4) {
  80. if(this.status == 200) {
  81. promise.success(this.responseText);
  82. } else {
  83. var errorData = {};
  84. errorData.message = this.status + " " + this.statusText;
  85. errorData.response = this.responseText;
  86. promise.failure(errorData);
  87. }
  88. }
  89. };
  90. xhttp.send();
  91. return promise;
  92. }
  93. function ajaxGetRaw(url, headers) {
  94. var promise = {"success": function() {},
  95. "failure": function() {}};
  96. var xhttp = new XMLHttpRequest();
  97. xhttp.open("GET", url, true);
  98. if(typeof headers != typeof undefined) {
  99. for (var key in headers) {
  100. xhttp.setRequestHeader(key, headers[key]);
  101. }
  102. }
  103. //xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  104. xhttp.onreadystatechange = function() {
  105. if (this.readyState == 4) {
  106. if(this.status == 200) {
  107. promise.success(this.responseText);
  108. } else {
  109. var errorData = {};
  110. errorData.message = this.status + " " + this.statusText;
  111. errorData.response = this.responseText;
  112. promise.failure(errorData);
  113. }
  114. }
  115. };
  116. xhttp.send();
  117. return promise;
  118. }