• 平台功能
  • 功能介绍
    • 配置文件
    • 系统函数
    • 组态图形
    • 页面集成
    • 功能扩展
    • SSL TLS
  • 本地部署
    • 数据库备份与还原
  • 协议处理
    • WebSocket
    • 国标协议
    • JT808协议
    • MQTT协议
    • OPC网关
    • 自定义协议(my)
    • 自定义协议(di)
    • 平台主动发给终端
    • 串口功能
    • 客户端功能
  • 完整案例演示
  • 系统函数源代码
  • 简洁的Ajax代码
    没有发现主题
文档主题
https://twuyun.com/help/doc/0/index?id=604cae4f9f4a38198822cb95
功能
模板管理图形管理脚本测试文档管理系统日志
0

简洁的Ajax代码

简洁的Ajax代码

  1. function initXMLhttp() {
  2. var e;
  3. return e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP")
  4. }
  5. function mysend(o, t, mypara) {
  6. o.send(t);
  7. }
  8. function minAjax(e) {
  9. caches.delete("threejs-editor");
  10. var mypara = e.data;
  11. var progress = e.progress;
  12. var cache = false;
  13. if (!e.url) return void (1 == e.debugLog && console.log("No Url!"));
  14. if (!e.type) return void (1 == e.debugLog && console.log(
  15. "No Default type (GET/POST) given!"));
  16. e.method != null || (e.method = !0),
  17. e.debugLog != null || (e.debugLog = !1);
  18. var o = initXMLhttp();
  19. if (e.rtype) {
  20. o.responseType = e.rtype;
  21. }
  22. var t = [],
  23. n = e.data;
  24. o.onprogress = function (e) {
  25. e = e || event;
  26. var total = e.total;
  27. if (total == 0) {
  28. try {
  29. total = e.srcElement.getResponseHeader('content-length');
  30. } catch (e) {
  31. console.error(e);
  32. }
  33. }
  34. // if (e.lengthComputable) {
  35. //result.innerHTML = "Received " + e.loaded + " of " + e.total + " bytes";
  36. if (progress) {
  37. progress(e.loaded, total, mypara);
  38. }
  39. // }
  40. };
  41. o.onreadystatechange = function () {
  42. if (4 == o.readyState && e.error && o.status != 200) {
  43. e.error(o.responseText);
  44. return;
  45. }
  46. if (4 == o.readyState && 200 == o.status) {
  47. //(e.success && e.success(o.responseText, o.readyState), 1 == e.debugLog && console.log("SuccessResponse"), 1 == e.debugLog && console.log("Response Data:" + o.responseText));
  48. if (cache) {
  49. var p = e.url + t;
  50. if (!window.mycaches) {
  51. window.mycaches = {};
  52. }
  53. window.mycaches[p] = o.responseText;
  54. checkresult(p, e.mypara);
  55. } else {
  56. if (e.rtype) {
  57. e.success && e.success(o.response, o.readyState);
  58. } else {
  59. (e.success && e.success(o.responseText, o.readyState), 1 == e.debugLog &&
  60. console.log("SuccessResponse"), 1 == e.debugLog && console.log(
  61. "Response Data:" + o.responseText));
  62. }
  63. }
  64. } else {
  65. 1 == e.debugLog && console.log("FailureResponse --> State:" + o.readyState +
  66. "Status:" + o.status);
  67. }
  68. // 4 == o.readyState && 200 == o.status ? (e.success && e.success(o.responseText, o.readyState), 1 == e.debugLog && console.log("SuccessResponse"), 1 == e.debugLog && console.log("Response Data:" + o.responseText)) : 1 == e.debugLog && console.log("FailureResponse --> State:" + o.readyState + "Status:" + o.status);
  69. };
  70. var contenttype = e.contenttype;
  71. if (contenttype == null) {
  72. contenttype = "application/x-www-form-urlencoded";
  73. }
  74. if ("string" == typeof n)
  75. for (var s = String.prototype.split.call(n, "&"), r = 0, a = s.length; a > r; r++) {
  76. var c = s[r].split("=");
  77. t.push(encodeURIComponent(c[0]) + "=" + encodeURIComponent(c[1]));
  78. t = t.join("&");
  79. } else if ("object" == typeof n && !(n instanceof String || FormData && n instanceof FormData)) {
  80. for (var p in n) {
  81. var c = n[p];
  82. if ("[object Array]" == Object.prototype.toString.call(c))
  83. for (var r = 0,
  84. a = c.length; a > r; r++) t.push(encodeURIComponent(p) + "[]=" +
  85. encodeURIComponent(c[r]));
  86. else t.push(encodeURIComponent(p) + "=" + (c == null ? "" : encodeURIComponent(
  87. c)))
  88. }
  89. t = t.join("&");
  90. } else {
  91. t = n;
  92. }
  93. "GET" == e.type && (o.open("GET", e.url + "?" + t, e.method), mysend(o), 1 == e
  94. .debugLog &&
  95. console.log("GET fired at:" + e.url + "?" + t)),
  96. "POST" == e.type && (o.open("POST", e.url, e.method), o.setRequestHeader(
  97. "Content-type",
  98. contenttype), mysend(o, t, mypara), 1 == e.debugLog &&
  99. console.log(
  100. "POST fired at:" + e.url + " || Data:" + t))
  101. }
  102. function ajax(method, url, data, success, error) {
  103. minAjax({
  104. url: url,
  105. type: method.toUpperCase(),
  106. data: data,
  107. success: function (data) {
  108. success && success(data);
  109. },
  110. error: function (data) {
  111. error && error(data);
  112. }
  113. });
  114. }
  115. function post(url, data, success, error, contenttype, rtype) {
  116. minAjax({
  117. rtype: rtype,
  118. contenttype: contenttype,
  119. url: url,
  120. type: "POST",
  121. data: data,
  122. success: function (data) {
  123. success && success(data);
  124. },
  125. error: function (data) {
  126. if (error) {
  127. error(data);
  128. } else {
  129. alert(data);
  130. }
  131. }
  132. });
  133. }