首页 > 微信 > 详细

小程序 wx.request请求

时间:2019-12-15 14:39:51      阅读:81      评论:0      收藏:0      [点我收藏+]

1.wx.request相当于发送ajax请求

微信官方解释

参数

属性 类型 默认值 必填 说明
url string 是 开发者服务器接口地址
data string/object/ArrayBuffer 否 请求的参数
header Object 否 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json
method string GET 否 HTTP 请求方法
dataType string json 否 返回的数据格式
responseType string text 否 响应的数据类型
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行

object.dataType 的合法值

值 说明
json 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse
其他 不对返回的内容进行 JSON.parse

object.success 回调函数

参数

Object res

属性 类型 说明
data string/Object/Arraybuffer 开发者服务器返回的数据
statusCode number 开发者服务器返回的 HTTP 状态码
header Object 开发者服务器返回的 HTTP Response Header

示例代码

wx.request({
  url: 'http://127.0.0.1:8000/test/', // 仅为示例,并非真实的接口地址
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // 默认值
  },
  method:'POST' //发送post请求
  success(res) {
    console.log(res.data)
  }
})

小程序 wx.request请求

原文:https://www.cnblogs.com/guapitomjoy/p/12044125.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!