ajax.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. function serialize( obj ) {
  2. return Object.keys(obj).map(k => 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. console.log(headers);
  73. if(typeof headers != typeof undefined) {
  74. for (var key in headers) {
  75. xhttp.setRequestHeader(key, headers[key]);
  76. }
  77. }
  78. //xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  79. xhttp.onreadystatechange = function() {
  80. if (this.readyState == 4) {
  81. if(this.status == 200) {
  82. console.log(this.responseText);
  83. } else {
  84. var errorData = {};
  85. errorData.message = this.status + " " + this.statusText;
  86. errorData.response = this.responseText;
  87. promise.failure(errorData);
  88. }
  89. }
  90. };
  91. xhttp.send();
  92. return promise;
  93. }