{"id":31097,"date":"2024-11-25T14:46:37","date_gmt":"2024-11-25T06:46:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31097\/"},"modified":"2024-11-25T14:46:37","modified_gmt":"2024-11-25T06:46:37","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8frequest%e7%bd%91%e7%bb%9c%e8%af%b7%e6%b1%82%e6%93%8d%e4%bd%9c%e5%ae%9e%e4%be%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31097\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8frequest\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\u5b9e\u4f8b\u8be6\u89e3"},"content":{"rendered":"<p>\u5c0f\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591aapi\uff0c\u6781\u5927\u7684\u65b9\u4fbf\u4e86\u5f00\u53d1\u8005\uff0c\u5176\u4e2d\u7f51\u7edc\u8bf7\u6c42api\u662fwx.request(object)\uff0c\u8fd9\u662f\u5c0f\u7a0b\u5e8f\u4e0e\u5f00\u53d1\u8005\u7684\u670d\u52a1\u5668\u5b9e\u73b0\u6570\u636e\u4ea4\u4e92\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684api\u3002\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4f7f\u7528request\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c,\u7ed3\u5408\u5b9e\u4f8b\u5f62\u5f0f\u5206\u6790\u4e86wx.request(object)\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\u7684\u5177\u4f53\u4f7f\u7528\u6280\u5de7,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002<\/p>\n<p>\u5b98\u65b9\u53c2\u6570\u8bf4\u660e\u5982\u4e0b<\/p>\n<p>OBJECT\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th width=\"91\"><span style=\"font-size: 14px\">\u53c2\u6570\u540d<\/span><\/th>\n<th width=\"112\"><span style=\"font-size: 14px\">\u7c7b\u578b<\/span><\/th>\n<th width=\"50\"><span style=\"font-size: 14px\">\u5fc5\u586b<\/span><\/th>\n<th width=\"367\"><span style=\"font-size: 14px\">\u8bf4\u660e<\/span><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">url<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">String<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u662f<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\u5f00\u53d1\u8005\u670d\u52a1\u5668\u63a5\u53e3\u5730\u5740<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">data<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">Object\u3001String<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u5426<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\u8bf7\u6c42\u7684\u53c2\u6570<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">header<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">Object<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u5426<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\u8bbe\u7f6e\u8bf7\u6c42\u7684 header , header \u4e2d\u4e0d\u80fd\u8bbe\u7f6e Referer<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">method<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">String<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u5426<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\u9ed8\u8ba4\u4e3a GET\uff0c\u6709\u6548\u503c\uff1aOPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">success<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">Function<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u5426<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\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;}<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">fail<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">Function<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u5426<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"91\"><span style=\"font-size: 14px\">complete<\/span><\/td>\n<td width=\"112\"><span style=\"font-size: 14px\">Function<\/span><\/td>\n<td width=\"50\"><span style=\"font-size: 14px\">\u5426<\/span><\/td>\n<td width=\"367\"><span style=\"font-size: 14px\">\u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u7b80\u5355\u7684\u7528\u6cd5\u5982\u4e0b\uff08\u4ee5POST\u8bf7\u6c42\u4e3a\u4f8b\uff09<\/p>\n<p><\/p>\n<pre>bindSearchChange:function(e){\r\n&nbsp;var&nbsp;keyword&nbsp;=&nbsp;e.detail.value;\r\n&nbsp;wx.request({\r\n&nbsp;url:'xxxxxxxxx',\r\n&nbsp;data:{},\r\n&nbsp;header:&nbsp;{'Content-Type':&nbsp;'application\/json'},\r\n&nbsp;success:&nbsp;function(res)&nbsp;{\r\n&nbsp;console.log(res)\r\n&nbsp;}\r\n&nbsp;})\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u628a\u8bf7\u6c42\u5199\u5728service\u6587\u4ef6\u4e0b\u7684http.js\u6587\u4ef6\u4e2d\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n<p><\/p>\n<pre>var&nbsp;rootDocment&nbsp;=&nbsp;'hxxxxx';\/\/\u4f60\u7684\u57df\u540d\r\nfunction&nbsp;req(url,data,cb){\r\n&nbsp;wx.request({\r\n&nbsp;url:&nbsp;rootDocment&nbsp;+&nbsp;url,\r\n&nbsp;data:&nbsp;data,\r\n&nbsp;method:&nbsp;'post',\r\n&nbsp;header:&nbsp;{'Content-Type':&nbsp;'application\/json'},\r\n&nbsp;success:&nbsp;function(res){\r\n&nbsp;return&nbsp;typeof&nbsp;cb&nbsp;==&nbsp;\"function\"&nbsp;&amp;amp;&amp;amp;&nbsp;cb(res.data)\r\n&nbsp;},\r\n&nbsp;fail:&nbsp;function(){\r\n&nbsp;return&nbsp;typeof&nbsp;cb&nbsp;==&nbsp;\"function\"&nbsp;&amp;amp;&amp;amp;&nbsp;cb(false)\r\n&nbsp;}\r\n&nbsp;})\r\n}\r\nmodule.exports&nbsp;=&nbsp;{\r\n&nbsp;req:&nbsp;req\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2dmodule.exports\u662f\u5c06req\u65b9\u6cd5\u66b4\u9732\u51fa\u53bb\u4f7f\u5f97\u522b\u7684\u6587\u4ef6\u4e2d\u53ef\u4ee5\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u7531\u4e8ejs\u51fd\u6570\u662f\u5f02\u6b65\u6267\u884c\u7684\uff0c\u6240\u4ee5return \u7684\u662f\u56de\u8c03\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5177\u4f53\u7684\u6570\u636e<\/p>\n<p>\u4e3a\u4e86\u5176\u4ed6\u6587\u4ef6\u65b9\u4fbf\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u6211\u4eec\u5728\u6839\u76ee\u5f55\u7684app.js\u6587\u4ef6\u4e2d\u5c06\u5176\u6ce8\u518c\u6210\u4e3a\u5168\u5c40\u51fd\u6570\uff0c\u5982\u4e0b<\/p>\n<p><\/p>\n<pre>\/\/app.js\r\nvar&nbsp;http&nbsp;=&nbsp;require('service\/http.js')\r\nApp({\r\n&nbsp;onLaunch:&nbsp;function&nbsp;()&nbsp;{\r\n&nbsp;\/\/\u8c03\u7528API\u4ece\u672c\u5730\u7f13\u5b58\u4e2d\u83b7\u53d6\u6570\u636e\r\n&nbsp;var&nbsp;logs&nbsp;=&nbsp;wx.getStorageSync('logs')&nbsp;||&nbsp;[]\r\n&nbsp;logs.unshift(Date.now())\r\n&nbsp;wx.setStorageSync('logs',&nbsp;logs)\r\n&nbsp;},\r\n&nbsp;getUserInfo:function(cb){\r\n&nbsp;var&nbsp;that&nbsp;=&nbsp;this\r\n&nbsp;if(this.globalData.userInfo){\r\n&nbsp;typeof&nbsp;cb&nbsp;==&nbsp;\"function\"&nbsp;&amp;amp;&amp;amp;&nbsp;cb(this.globalData.userInfo)\r\n&nbsp;}else{\r\n&nbsp;\/\/\u8c03\u7528\u767b\u5f55\u63a5\u53e3\r\n&nbsp;wx.login({\r\n&nbsp;success:&nbsp;function&nbsp;()&nbsp;{\r\n&nbsp;&nbsp;wx.getUserInfo({\r\n&nbsp;&nbsp;success:&nbsp;function&nbsp;(res)&nbsp;{\r\n&nbsp;&nbsp;that.globalData.userInfo&nbsp;=&nbsp;res.userInfo\r\n&nbsp;&nbsp;typeof&nbsp;cb&nbsp;==&nbsp;\"function\"&nbsp;&amp;amp;&amp;amp;&nbsp;cb(that.globalData.userInfo)\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;})\r\n&nbsp;}\r\n&nbsp;})\r\n&nbsp;}\r\n&nbsp;},\r\n&nbsp;globalData:{\r\n&nbsp;userInfo:null\r\n&nbsp;},\r\n&nbsp;func:{\r\n&nbsp;req:http.req\r\n&nbsp;}\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u65f6\u8fd9\u4e2areq\u5c31\u662f\u5168\u5c40\u7684\u4e86\uff0c\u5728\u8c03\u7528\u65f6\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528getApp.func.req()\u6765\u8c03\u7528\uff0c\u5177\u4f53\u5982\u4e0b<\/p>\n<p><\/p>\n<pre>var&nbsp;app&nbsp;=&nbsp;getApp()\r\nPage({\r\n&nbsp;data:&nbsp;{\r\n&nbsp;},\r\n&nbsp;onLoad:&nbsp;function&nbsp;(opt)&nbsp;{\r\n&nbsp;\/\/console.log(opt.name)\r\n&nbsp;app.func.req('\/api\/get_data',{},function(res){\r\n&nbsp;console.log(res)\r\n&nbsp;});\r\n&nbsp;}\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591aapi\uff0c\u5305\u62ec\u7f51\u7edc\uff0c\u5a92\u4f53\uff0c\u6570\u636e\u7b49\uff0c\u4e5f\u63d0\u4f9b\u4e86\u5f88\u591a\u7ec4\u4ef6\uff0c\u4f7f\u5f00\u53d1\u5c0f\u7a0b\u5e8f\u53d8\u5f97\u5f88\u65b9\u4fbf\u3002<\/p>\n<p>\u76f8\u5173\u63a8\u8350\uff1a<\/p>\n<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8frequest\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\u5b9e\u4f8b\u8be6\u89e3\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>\u5c0f\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f88\u591aapi\uff0c\u6781\u5927\u7684\u65b9\u4fbf\u4e86\u5f00\u53d1\u8005\uff0c\u5176\u4e2d\u7f51\u7edc\u8bf7\u6c42api\u662fwx.request(object)\uff0c\u8fd9\u662f\u5c0f\u7a0b\u5e8f\u4e0e\u5f00\u53d1\u8005\u7684\u670d\u52a1\u5668\u5b9e\u73b0\u6570\u636e\u4ea4\u4e92\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684api\u3002\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4f7f\u7528request\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c,\u7ed3\u5408\u5b9e\u4f8b\u5f62\u5f0f\u5206\u6790\u4e86wx.request(object)\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\u7684\u5177\u4f53\u4f7f\u7528\u6280\u5de7,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002 \u5b98\u65b9\u53c2\u6570\u8bf4\u660e\u5982\u4e0b OBJECT\u53c2\u6570\u8bf4\u660e\uff1a \u53c2\u6570\u540d \u7c7b\u578b \u5fc5\u586b \u8bf4\u660e url String \u662f \u5f00\u53d1\u8005\u670d\u52a1\u5668\u63a5\u53e3\u5730\u5740 data Object\u3001String \u5426 \u8bf7\u6c42\u7684\u53c2\u6570 header Object \u5426 \u8bbe\u7f6e\u8bf7\u6c42\u7684 header , header \u4e2d\u4e0d\u80fd\u8bbe\u7f6e Referer method String \u5426 \u9ed8\u8ba4\u4e3a GET\uff0c\u6709\u6548\u503c\uff1aOPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT success Function \u5426 \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;} fail Function \u5426 \u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570 complete Function \u5426 \u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09 \u6700\u7b80\u5355\u7684\u7528\u6cd5\u5982\u4e0b\uff08\u4ee5POST\u8bf7\u6c42\u4e3a\u4f8b\uff09 bindSearchChange:function(e){ &nbsp;var&nbsp;keyword&nbsp;=&nbsp;e.detail.value; &nbsp;wx.request({ [&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-31097","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31097","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=31097"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31097\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}