{"id":30392,"date":"2024-11-25T13:24:44","date_gmt":"2024-11-25T05:24:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30392\/"},"modified":"2024-11-25T13:24:44","modified_gmt":"2024-11-25T05:24:44","slug":"%e6%80%8e%e4%b9%88%e7%8e%a9%e8%bd%ac%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e7%9a%84ajax%e6%95%b0%e6%8d%ae%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30392\/","title":{"rendered":"\u600e\u4e48\u73a9\u8f6c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684ajax\u6570\u636e\u8bf7\u6c42"},"content":{"rendered":"<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684ajax\u6570\u636e\u8bf7\u6c42\uff0c\u5f88\u591a\u540c\u5b66\u627e\u4e0d\u5230api\u5728\u54ea\u4e2a\u4f4d\u7f6e\uff0c\u8fd9\u91cc\u5355\u72ec\u628a\u5c0f\u7a0b\u5e8f\u7684ajax\u8bf7\u6c42\u7ed9\u5217\u51fa\u6765\uff0c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u8bf7\u6c42\u5c31\u662fwx.request\u8fd9\u4e2aapi\uff0cwx.request(\u4e00\u4e9b\u5bf9\u8c61\u53c2\u6570)\uff0c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e0d\u540c\u4e8e\u6d4f\u89c8\u5668\u7684ajax\u8bf7\u6c42\uff0c\u53ef\u4ee5\u76f4\u63a5\u8de8\u57df\u8bf7\u6c42\u4e0d\u7528\u8003\u8651\u8de8\u57df\u95ee\u9898\u3002<\/p>\n<p style=\"margin-top: 30px;margin-bottom: 14px;font-weight: 400;position: relative;line-height: 1em;font-size: 22px;font-family:\">\u4f7f\u7528\u5c0f\u7a0b\u5e8f\u5b98\u65b9\u63d0\u4f9b\u7684\u6570\u636e\u8bf7\u6c42api\u53d1\u8d77\u6570\u636e\u8bf7\u6c42<\/p>\n<p style=\"margin-top: 24px;margin-bottom: 12px;font-weight: 400;position: relative;line-height: 1em;font-size: 20px;font-family:\">wx.request(OBJECT)<\/p>\n<p>wx.request\u53d1\u8d77\u7684\u662fhttps\u8bf7\u6c42\u3002\u4e00\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u540c\u65f6\u53ea\u80fd\u67095\u4e2a\u7f51\u7edc\u8bf7\u6c42\u8fde\u63a5\u3002<\/p>\n<p>OBJECT\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<table border=\"1px\">\n<thead>\n<tr>\n<th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u53c2\u6570\u540d<\/th>\n<th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u7c7b\u578b<\/th>\n<th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5fc5\u586b<\/th>\n<th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u662f<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5f00\u53d1\u8005\u670d\u52a1\u5668\u63a5\u53e3\u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object\u3001String<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5426<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u8bf7\u6c42\u7684\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5426<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u8bbe\u7f6e\u8bf7\u6c42\u7684 header , header \u4e2d\u4e0d\u80fd\u8bbe\u7f6e Referer<\/td>\n<\/tr>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5426<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u9ed8\u8ba4\u4e3a GET\uff0c\u6709\u6548\u503c\uff1aOPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT<\/td>\n<\/tr>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5426<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u6536\u5230\u5f00\u53d1\u8005\u670d\u52a1\u6210\u529f\u8fd4\u56de\u7684\u56de\u8c03\u51fd\u6570\uff0cres = {data: &#8216;\u5f00\u53d1\u8005\u670d\u52a1\u5668\u8fd4\u56de\u7684\u5185\u5bb9&#8217;}<\/td>\n<\/tr>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fail<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5426<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; complete<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5426<\/td>\n<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>wx.request({\r\n&nbsp;&nbsp;url:&nbsp;'test.php',\r\n&nbsp;&nbsp;data:&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x:&nbsp;''&nbsp;,\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y:&nbsp;''\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;header:&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Content-Type':&nbsp;'application\/json'\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;success:&nbsp;function(res)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(res.data)\r\n&nbsp;&nbsp;}\r\n})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-top: 30px;margin-bottom: 14px;font-weight: 400;position: relative;line-height: 1em;font-size: 22px;font-family:\">\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528fetch\u505aajax\u8bf7\u6c42<\/p>\n<p>fetch\u662f\u4e00\u79cd\u65b0\u7684ajax\u8bf7\u6c42\u89c4\u8303\uff0cfetch\u5728\u5c0f\u7a0b\u5e8f\u4e2d\u4e5f\u662f\u652f\u6301\u7684\uff0c\u6d4b\u8bd5ajax\u8bf7\u6c42\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>then\u4e2d\u5e26\u4ee3\u7801\u662f\u6d4b\u8bd5\uff0c\u8fd9\u91cc\u662f\u8282\u9009\u4e86\u5c0f\u90e8\u5206\u4ee3\u7801\uff0c\u5b9e\u9645\u4f7f\u7528\u9700\u8981\u81ea\u884c\u4fee\u6539\u3002<\/p>\n<pre>fetch('http:\/\/www.php.cn\/json.php?typeid=34&amp;amp;page=1&amp;amp;pagesize=10')\r\n&nbsp;&nbsp;&nbsp;&nbsp;.then(function(response){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(response.status==200){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that.data.page++;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;response.json();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;}).then(function(data){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(data);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u66f4\u65b0\u6570\u636e\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that.setData({\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listArr:that.data.page==1&nbsp;?&nbsp;data&nbsp;:&nbsp;that.data.listArr.concat(data)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(that.data.listArr);\r\n&nbsp;&nbsp;&nbsp;&nbsp;})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u73a9\u8f6c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684ajax\u6570\u636e\u8bf7\u6c42\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684ajax\u6570\u636e\u8bf7\u6c42\uff0c\u5f88\u591a\u540c\u5b66\u627e\u4e0d\u5230api\u5728\u54ea\u4e2a\u4f4d\u7f6e\uff0c\u8fd9\u91cc\u5355\u72ec\u628a\u5c0f\u7a0b\u5e8f\u7684ajax\u8bf7\u6c42\u7ed9\u5217\u51fa\u6765\uff0c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u8bf7\u6c42\u5c31\u662fwx.request\u8fd9\u4e2aapi\uff0cwx.request(\u4e00\u4e9b\u5bf9\u8c61\u53c2\u6570)\uff0c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e0d\u540c\u4e8e\u6d4f\u89c8\u5668\u7684ajax\u8bf7\u6c42\uff0c\u53ef\u4ee5\u76f4\u63a5\u8de8\u57df\u8bf7\u6c42\u4e0d\u7528\u8003\u8651\u8de8\u57df\u95ee\u9898\u3002 \u4f7f\u7528\u5c0f\u7a0b\u5e8f\u5b98\u65b9\u63d0\u4f9b\u7684\u6570\u636e\u8bf7\u6c42api\u53d1\u8d77\u6570\u636e\u8bf7\u6c42 wx.request(OBJECT) wx.request\u53d1\u8d77\u7684\u662fhttps\u8bf7\u6c42\u3002\u4e00\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u540c\u65f6\u53ea\u80fd\u67095\u4e2a\u7f51\u7edc\u8bf7\u6c42\u8fde\u63a5\u3002 OBJECT\u53c2\u6570\u8bf4\u660e\uff1a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u53c2\u6570\u540d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u7c7b\u578b &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5fc5\u586b &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u8bf4\u660e &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-30392","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=30392"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30392\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}