Skip to main content

小程序 微信中的请求

wx.request#

它是微信内置的请求接口的方法,具体配置和jq的ajax类似 参数有:

属性类型说明
urlstring开发者服务器接口地址
datastring/object/ArrayBuffer请求的参数
headerObject设置请求的 header,header 中不能设置 Referer。
content-typeapplication/json
methodstringHTTP 请求方法
dataTypestring返回的数据格式
responseTypestring响应的数据类型
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

取消请求#

  1. 设置一个全局变量
var glob = null;Page({})
  1. 将wx.request 赋给全局变量,
glob = wx.request({    url: "http://127.0.0.1:5500/router/api/data.json",    // 成功    success: function (data) {        console.log(data);        // 停止下拉刷新        wx.stopPullDownRefresh()    },    // 失败    fail: function () {        // 如果失败了,强制下拉刷新        wx.startPullDownRefresh()    }});
  1. 取消
onUnload: function () {    if (glob) {        glob.abort()    }},