[关闭]
@Chiang 2021-09-25T17:50:01.000000Z 字数 3694 阅读 532

ecshop 里的 ajax 请求与响应

2021-09 ecshop Ajax.call


先来看看文件在哪儿 transport.js

项目中出现好些个这种写法

  1. Ajax.call('goods.php?act=gotopage', 'page=' + page + '&id=' + id, gotoBuyPageResponse, 'GET', 'JSON');

原来是别名

  1. /* 定义两个别名 */
  2. var Ajax = Transport;
  3. Ajax.call = Transport.run;

具体看看 Transport.run

  1. /* *
  2. * 调用此方法发送HTTP请求。
  3. *
  4. * @public
  5. * @param {string} url 请求的URL地址
  6. * @param {mix} params 发送参数
  7. * @param {Function} callback 回调函数
  8. * @param {string} ransferMode 请求的方式,有"GET"和"POST"两种
  9. * @param {string} responseType 响应类型,有"JSON"、"XML"和"TEXT"三种
  10. * @param {boolean} asyn 是否异步请求的方式
  11. * @param {boolean} quiet 是否安静模式请求
  12. */
  13. run : function (url, params, callback, transferMode, responseType, asyn, quiet){
  14. /* 处理用户在调用该方法时输入的参数 */
  15. params = this.parseParams(params);
  16. transferMode = typeof(transferMode) === "string" && transferMode.toUpperCase() === "GET" ? "GET" : "POST";
  17. if (transferMode === "GET")
  18. {
  19. var d = new Date();
  20. url += params ? (url.indexOf("?") === - 1 ? "?" : "&") + params : "";
  21. url = encodeURI(url) + (url.indexOf("?") === - 1 ? "?" : "&") + d.getTime() + d.getMilliseconds();
  22. params = null;
  23. }
  24. responseType = typeof(responseType) === "string" && ((responseType = responseType.toUpperCase()) === "JSON" || responseType === "XML") ? responseType : "TEXT";
  25. asyn = asyn === false ? false : true;
  26. /* 处理HTTP请求和响应 */
  27. var xhr = this.createXMLHttpRequest();
  28. try
  29. {
  30. var self = this;
  31. if (typeof(self.onRunning) === "function" && !quiet)
  32. {
  33. self.onRunning();
  34. }
  35. xhr.open(transferMode, url, asyn);
  36. if (transferMode === "POST")
  37. {
  38. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  39. }
  40. if (asyn)
  41. {
  42. xhr.onreadystatechange = function ()
  43. {
  44. if (xhr.readyState == 4)
  45. {
  46. switch ( xhr.status )
  47. {
  48. case 0:
  49. case 200: // OK!
  50. /*
  51. * If the request was to create a new resource
  52. * (such as post an item to the database)
  53. * You could instead return a status code of '201 Created'
  54. */
  55. if (typeof(self.onComplete) === "function")
  56. {
  57. self.onComplete();
  58. }
  59. if (typeof(callback) === "function")
  60. {
  61. callback.call(self, self.parseResult(responseType, xhr), xhr.responseText);
  62. }
  63. break;
  64. case 304: // Not Modified
  65. /*
  66. * This would be used when your Ajax widget is
  67. * checking for updated content,
  68. * such as the Twitter interface.
  69. */
  70. break;
  71. case 400: // Bad Request
  72. /*
  73. * A bit like a safety net for requests by your JS interface
  74. * that aren't supported on the server.
  75. * "Your browser made a request that the server cannot understand"
  76. */
  77. //alert("XmlHttpRequest status: [400] Bad Request");//by dmc
  78. break;
  79. case 404: // Not Found
  80. //alert("XmlHttpRequest status: [404] \nThe requested URL "+url+" was not found on this server.");// by dmc
  81. break;
  82. case 409: // Conflict
  83. /*
  84. * Perhaps your JavaScript request attempted to
  85. * update a Database record
  86. * but failed due to a conflict
  87. * (eg: a field that must be unique)
  88. */
  89. break;
  90. case 503: // Service Unavailable
  91. /*
  92. * A resource that this request relies upon
  93. * is currently unavailable
  94. * (eg: a file is locked by another process)
  95. */
  96. //alert("XmlHttpRequest status: [503] Service Unavailable"); //by dmc
  97. break;
  98. default:
  99. //alert("XmlHttpRequest status: [" + xhr.status + "] Unknow status."); //by dmc
  100. }
  101. xhr = null;
  102. }
  103. }
  104. if (xhr != null) xhr.send(params);
  105. }
  106. else
  107. {
  108. if (typeof(self.onRunning) === "function")
  109. {
  110. self.onRunning();
  111. }
  112. xhr.send(params);
  113. var result = self.parseResult(responseType, xhr);
  114. //xhr = null;
  115. if (typeof(self.onComplete) === "function")
  116. {
  117. self.onComplete();
  118. }
  119. if (typeof(callback) === "function")
  120. {
  121. callback.call(self, result, xhr.responseText);
  122. }
  123. return result;
  124. }
  125. }
  126. catch (ex)
  127. {
  128. if (typeof(self.onComplete) === "function")
  129. {
  130. self.onComplete();
  131. }
  132. }
  133. },

如何响应

  1. /**
  2. * @access public
  3. * @param
  4. * @return void
  5. */
  6. function make_json_result($content, $message='', $append=array())
  7. {
  8. make_json_response($content, 0, $message, $append);
  9. }
  10. /**
  11. * 创建一个JSON格式的数据
  12. *
  13. * @access public
  14. * @param string $content
  15. * @param integer $error
  16. * @param string $message
  17. * @param array $append
  18. * @return void
  19. */
  20. function make_json_response($content='', $error="0", $message='', $append=array(), $go2url = '')
  21. {
  22. include_once(ROOT_PATH . 'includes/cls_json.php');
  23. $json = new JSON;
  24. $res = array('error' => $error, 'message' => $message, 'content' => $content, 'go2url' => $go2url);
  25. if (!empty($append))
  26. {
  27. foreach ($append AS $key => $val)
  28. {
  29. $res[$key] = $val;
  30. }
  31. }
  32. $val = $json->encode($res);
  33. exit($val);
  34. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注