开拓者制服组 發表於 2020-8-27 14:09:00

uni-app-网络请求

<p>官方文档</p>
<p>&nbsp;</p>
<h3 id="request">uni.request(OBJECT)</h3>
<p>发起网络请求。</p>
<blockquote>
<p>在各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。</p>
</blockquote>
<p>OBJECT 参数说明</p>
<table>
<thead>
<tr><th>参数名</th><th>类型</th><th>必填</th><th>默认值</th><th>说明</th><th>平台差异说明</th></tr>
</thead>
<tbody>
<tr>
<td>url</td>
<td>String</td>
<td>是</td>
<td>&nbsp;</td>
<td>开发者服务器接口地址</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>data</td>
<td>Object/String/ArrayBuffer</td>
<td>否</td>
<td>&nbsp;</td>
<td>请求的参数</td>
<td>App(自定义组件编译模式)不支持ArrayBuffer类型</td>
</tr>
<tr>
<td>header</td>
<td>Object</td>
<td>否</td>
<td>&nbsp;</td>
<td>设置请求的 header,header 中不能设置 Referer。</td>
<td>H5端会自动带上cookie不可手动覆盖</td>
</tr>
<tr>
<td>method</td>
<td>String</td>
<td>否</td>
<td>GET</td>
<td>有效值详见下方说明</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>timeout</td>
<td>Number</td>
<td>否</td>
<td>30000</td>
<td>超时时间,单位 ms</td>
<td>微信小程序(2.10.0)、支付宝小程序</td>
</tr>
<tr>
<td>dataType</td>
<td>String</td>
<td>否</td>
<td>json</td>
<td>如果设为 json,会尝试对返回的数据做一次 JSON.parse</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>responseType</td>
<td>String</td>
<td>否</td>
<td>text</td>
<td>设置响应的数据类型。合法值:text、arraybuffer</td>
<td>App和支付宝小程序不支持</td>
</tr>
<tr>
<td>sslVerify</td>
<td>Boolean</td>
<td>否</td>
<td>true</td>
<td>验证 ssl 证书</td>
<td>仅App安卓端支持(HBuilderX 2.3.3+)</td>
</tr>
<tr>
<td>withCredentials</td>
<td>Boolean</td>
<td>否</td>
<td>false</td>
<td>跨域请求时是否携带凭证(cookies)</td>
<td>仅H5支持(HBuilderX 2.6.15+)</td>
</tr>
<tr>
<td>firstIpv4</td>
<td>Boolean</td>
<td>否</td>
<td>false</td>
<td>DNS解析时优先使用ipv4</td>
<td>仅 App-Android 支持 (HBuilderX 2.8.0+)</td>
</tr>
<tr>
<td>success</td>
<td>Function</td>
<td>否</td>
<td>&nbsp;</td>
<td>收到开发者服务器成功返回的回调函数</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>fail</td>
<td>Function</td>
<td>否</td>
<td>&nbsp;</td>
<td>接口调用失败的回调函数</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>complete</td>
<td>Function</td>
<td>否</td>
<td>&nbsp;</td>
<td>接口调用结束的回调函数(调用成功、失败都会执行)</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>method 有效值</p>
<p>必须大写,有效值在不同平台差异说明不同。</p>
<table>
<thead>
<tr><th>method</th><th>App</th><th>H5</th><th>微信小程序</th><th>支付宝小程序</th><th>百度小程序</th><th>字节跳动小程序</th></tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
</tr>
<tr>
<td>POST</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
</tr>
<tr>
<td>PUT</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>x</td>
<td>√</td>
<td>√</td>
</tr>
<tr>
<td>DELETE</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>x</td>
<td>√</td>
<td>x</td>
</tr>
<tr>
<td>CONNECT</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>HEAD</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>x</td>
<td>√</td>
<td>x</td>
</tr>
<tr>
<td>OPTIONS</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>x</td>
<td>√</td>
<td>x</td>
</tr>
<tr>
<td>TRACE</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
</tbody>
</table>
<p>success 返回参数说明</p>
<table>
<thead>
<tr><th>参数</th><th>类型</th><th>说明</th></tr>
</thead>
<tbody>
<tr>
<td>data</td>
<td>Object/String/ArrayBuffer</td>
<td>开发者服务器返回的数据</td>
</tr>
<tr>
<td>statusCode</td>
<td>Number</td>
<td>开发者服务器返回的 HTTP 状态码</td>
</tr>
<tr>
<td>header</td>
<td>Object</td>
<td>开发者服务器返回的 HTTP Response Header</td>
</tr>
<tr>
<td>cookies</td>
<td><code>Array.&lt;string&gt;</code></td>
<td>开发者服务器返回的 cookies,格式为字符串数组</td>
</tr>
</tbody>
</table>
<p>data 数据说明</p>
<p>最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:</p>
<ul>
<li>对于&nbsp;<code>GET</code>&nbsp;方法,会将数据转换为 query string。例如&nbsp;<code>{ name: 'name', age: 18 }</code>&nbsp;转换后的结果是&nbsp;<code>name=name&amp;age=18</code>。</li>
<li>对于&nbsp;<code>POST</code>&nbsp;方法且&nbsp;<code>header['content-type']</code>&nbsp;为&nbsp;<code>application/json</code>&nbsp;的数据,会进行 JSON 序列化。</li>
<li>对于&nbsp;<code>POST</code>&nbsp;方法且&nbsp;<code>header['content-type']</code>&nbsp;为&nbsp;<code>application/x-www-form-urlencoded</code>&nbsp;的数据,会将数据转换为 query string。</li>
</ul>
<p>示例</p>
<pre data-lang="javascript"><code class="lang-javascript">uni<span class="token punctuation">.<span class="token function">request<span class="token punctuation">(<span class="token punctuation">{
    url<span class="token punctuation">: <span class="token string">'https://www.example.com/request'<span class="token punctuation">, <span class="token comment">//仅为示例,并非真实接口地址。
    data<span class="token punctuation">: <span class="token punctuation">{
      text<span class="token punctuation">: <span class="token string">'uni.request'
    <span class="token punctuation">}<span class="token punctuation">,
    header<span class="token punctuation">: <span class="token punctuation">{
      <span class="token string">'custom-header'<span class="token punctuation">: <span class="token string">'hello' <span class="token comment">//自定义请求头信息
    <span class="token punctuation">}<span class="token punctuation">,
    success<span class="token punctuation">: <span class="token punctuation">(res<span class="token punctuation">) <span class="token operator">=&gt; <span class="token punctuation">{
      console<span class="token punctuation">.<span class="token function">log<span class="token punctuation">(res<span class="token punctuation">.data<span class="token punctuation">)<span class="token punctuation">;
      <span class="token keyword">this<span class="token punctuation">.text <span class="token operator">= <span class="token string">'request success'<span class="token punctuation">;
    <span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">)<span class="token punctuation">;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>返回值</p>
<p>如果希望返回一个&nbsp;<code>requestTask</code>&nbsp;对象,需要至少传入 success / fail / complete 参数中的一个。例如:</p>
<pre data-lang="javascript"><code class="lang-javascript"><span class="token keyword">var requestTask <span class="token operator">= uni<span class="token punctuation">.<span class="token function">request<span class="token punctuation">(<span class="token punctuation">{
    url<span class="token punctuation">: <span class="token string">'https://www.example.com/request'<span class="token punctuation">, <span class="token comment">//仅为示例,并非真实接口地址。
    complete<span class="token punctuation">: <span class="token punctuation">(<span class="token punctuation">)<span class="token operator">=&gt; <span class="token punctuation">{<span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">)<span class="token punctuation">;
requestTask<span class="token punctuation">.<span class="token function">abort<span class="token punctuation">(<span class="token punctuation">)<span class="token punctuation">;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>如果没有传入 success / fail / complete 参数,则会返回封装后的 Promise 对象:Promise 封装</p>
<p>通过&nbsp;<code>requestTask</code>,可中断请求任务。</p>
<p>requestTask 对象的方法列表</p>
<table>
<thead>
<tr><th>方法</th><th>参数</th><th>说明</th></tr>
</thead>
<tbody>
<tr>
<td>abort</td>
<td>&nbsp;</td>
<td>中断请求任务</td>
</tr>
<tr>
<td>offHeadersReceived</td>
<td>&nbsp;</td>
<td>取消监听 HTTP Response Header 事件,仅<code>微信小程序平台</code>支持,文档详情</td>
</tr>
<tr>
<td>onHeadersReceived</td>
<td>&nbsp;</td>
<td>监听 HTTP Response Header 事件。会比请求完成事件更早,仅<code>微信小程序平台</code>支持,文档详情</td>
</tr>
</tbody>
</table>
<p>示例</p>
<pre data-lang="javascript"><code class="lang-javascript"><span class="token keyword">const requestTask <span class="token operator">= uni<span class="token punctuation">.<span class="token function">request<span class="token punctuation">(<span class="token punctuation">{
    url<span class="token punctuation">: <span class="token string">'https://www.example.com/request'<span class="token punctuation">, <span class="token comment">//仅为示例,并非真实接口地址。
    data<span class="token punctuation">: <span class="token punctuation">{
      name<span class="token punctuation">: <span class="token string">'name'<span class="token punctuation">,
      age<span class="token punctuation">: <span class="token number">18
    <span class="token punctuation">}<span class="token punctuation">,
    success<span class="token punctuation">: <span class="token keyword">function<span class="token punctuation">(res<span class="token punctuation">) <span class="token punctuation">{
      console<span class="token punctuation">.<span class="token function">log<span class="token punctuation">(res<span class="token punctuation">.data<span class="token punctuation">)<span class="token punctuation">;
    <span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">)<span class="token punctuation">;

<span class="token comment">// 中断请求任务
requestTask<span class="token punctuation">.<span class="token function">abort<span class="token punctuation">(<span class="token punctuation">)<span class="token punctuation">;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>Tips</p>
<ul>
<li>请求的&nbsp;<code>header</code>&nbsp;中&nbsp;<code>content-type</code>&nbsp;默认为&nbsp;<code>application/json</code>。</li>
<li>避免在&nbsp;<code>header</code>&nbsp;中使用中文,或者使用 encodeURIComponent 进行编码,否则在百度小程序报错。(来自:快狗打车前端团队)</li>
<li>网络请求的&nbsp;<code>超时时间</code>&nbsp;可以统一在&nbsp;<code>manifest.json</code>&nbsp;中配置&nbsp;networkTimeout。</li>
<li>H5 端本地调试需注意跨域问题,参考:调试跨域问题解决方案</li>
<li>注意由于百度小程序iOS客户端,请求失败时会进入fail回调,需要针对百度增加相应的处理以解决该问题。</li>
<li>注意非 H5 端不支持自动保持 cookie,服务器应避免验证 cookie。如果服务器无法修改,也可以使用一些模拟手段,比如这样的工具https://github.com/charleslo1/weapp-cookie&nbsp;可以请求时带上 cookie 并将响应的 cookie 保存在本地。</li>
<li>H5端 cookie 受跨域限制(和平时开发网站时一样),但 uni.request 目前未支持 withCredentials 配置,可以直接使用 xhr 对象或者其他类库。</li>
<li>根据 W3C 规范,H5 端无法获取 response header 中 Set-Cookie、Set-Cookie2 这2个字段,对于跨域请求,允许获取的 response header 字段只限于“simple response header”和“Access-Control-Expose-Headers”(详情)</li>
<li>uni-app 插件市场有flyio、axios等三方封装的拦截器可用</li>
<li>低版本手机自身不支持 ipv6,如果服务器仅允许 ipv6,会导致老手机无法正常运行或访问速度非常慢</li>
<li>localhost、127.0.0.1等服务器地址,只能在电脑端运行,手机端连接时不能访问。请使用标准IP并保证手机能连接电脑网络</li>
<li>debug 模式,安卓端暂时无法获取响应头,url中含有非法字符(如未编码为%20的空格)时会请求失败</li>
<li>iOS App第一次安装启动后,会弹出是否允许联网的询问框,在用户点击同意前,调用联网API会失败。请注意判断这种情况。比如官方提供的新闻模板示例(HBuilderX新建项目可选择),会判断如果无法联网,则提供一个错误页,提示用户设置网络及下拉刷新重试。</li>
<li>良好体验的App,还会判断当前是否处于飞行模式(参考)、是wifi还是3G(参考)</li>
<li>部分安卓设备,真机运行或debug模式下的网络,低于release模式很多。</li>
<li>使用一些比较小众的证书机构(如:CFCA OV OCA)签发的 ssl 证书在安卓设备请求会失败,因为这些机构的根证书不在系统内置根证书库,可以更换其他常见机构签发的证书(如:Let's Encrypt),或者配置 sslVerify 为 false 关闭 ssl 证书验证(不推荐)。</li>
<li>单次网络请求数据量建议控制在50K以下(仅指json数据,不含图片),过多数据应分页获取,以提升应用体验。</li>
</ul>
<div class="cnblogs_code">
<pre>&lt;template&gt;
    &lt;view&gt;
      &lt;view&gt;
            &lt;text selectable&gt;唱跳Rap篮球{{name}}&lt;/text&gt;
      &lt;/view&gt;
      &lt;view&gt;
            &lt;text selectable space=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">emsp</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;唱跳 Rap 篮球-{{name}}&lt;/text&gt;
      &lt;/view&gt;
      &lt;view <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">box</span><span style="color: rgba(128, 0, 0, 1)">"</span> hover-<span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">box-active</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
            &lt;!-- hover-stop-propagation阻止冒泡 --&gt;
            &lt;view <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">box1</span><span style="color: rgba(128, 0, 0, 1)">"</span> :hover-start-time=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2000</span><span style="color: rgba(128, 0, 0, 1)">"</span> :hover-stay-time=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2000</span><span style="color: rgba(128, 0, 0, 1)">"</span> hover-<span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">box1-active</span><span style="color: rgba(128, 0, 0, 1)">"</span> hover-stop-propagation&gt;&lt;/view&gt;
      &lt;/view&gt;
      &lt;view&gt;
            &lt;text&gt;{{btn}}&lt;/text&gt;
            &lt;button @click=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">set_btn()</span><span style="color: rgba(128, 0, 0, 1)">"</span> size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">default</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">primary</span><span style="color: rgba(128, 0, 0, 1)">"</span> plain loading&gt;按钮&lt;/button&gt;
      &lt;/view&gt;
      &lt;image src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://img.gucci-outlets-online.com:8088/catch_down/20200427124446_683364.gif</span><span style="color: rgba(128, 0, 0, 1)">"</span> mode=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">aspectFit</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/image&gt;
      &lt;view v-<span style="color: rgba(0, 0, 255, 1)">for</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">(item,index) in arr</span><span style="color: rgba(128, 0, 0, 1)">"</span> :key=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">item.id</span><span style="color: rgba(128, 0, 0, 1)">'</span>&gt;
            &lt;view <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gogogo</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;<span style="color: rgba(0, 0, 0, 1)">
                {{index}}</span>-<span style="color: rgba(0, 0, 0, 1)">{{item.title}}year{{item.year}}
                </span>&lt;!-- 事迹{{item.content}} --&gt;
            &lt;/view&gt;
      &lt;/view&gt;
    &lt;/view&gt;
&lt;/template&gt;

&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
      data() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
                name: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">菜菜</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                btn: </span><span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">,
                arr: [],
            }
      },
      methods: {
            set_btn() {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.btn = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">爱了爱了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.send_Get()
            },
            send_Get() {
                let _this </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">
                uni.request({
                  method: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">GET</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                  url: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://api.jisuapi.com/todayhistory/query</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                  data: {
                        appkey: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">d1e87a8e45ba7749</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                        month: </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">,
                        day: </span><span style="color: rgba(128, 0, 128, 1)">18</span><span style="color: rgba(0, 0, 0, 1)">
                  },
                  success(res) {
                        console.log(res.data)
                        console.log(res.data.result)
                        _this.arr </span>=<span style="color: rgba(0, 0, 0, 1)"> res.data.result
                  },
                })
            }
      },
      onPullDownRefresh() {
            console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">刷新了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      },
      onReachBottom() {
            console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">触底了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      }
    }
</span>&lt;/script&gt;

&lt;style&gt;<span style="color: rgba(0, 0, 0, 1)">
    .box {
      width: 200px;
      height: 200px;
      background: #007AFF;
    }

    .box</span>-<span style="color: rgba(0, 0, 0, 1)">active {
      background: red;
    }

    .box1 {
      width: 100px;
      height: 100px;
      background: #4CD964;
    }

    .box1</span>-<span style="color: rgba(0, 0, 0, 1)">active {
      background: white;
    }

    .gogogo {
      margin: 30px auto;
      </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> padding: 50px 30px; </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    }
</span>&lt;/style&gt;</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/fwjlucifinil/p/13571107.html
頁: [1]
查看完整版本: uni-app-网络请求