微信小程序发起请求wx.request

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.success 回调函数

参数

Object res

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

wxml

 <button bindtap="qing">请求</button>

js

qing:function(){
    wx.request({
      url: 'http://127.0.0.1:8000/test/', //路由
      data:{   //数据
        "name":"egon"
      },
      method:"POST",//请求方法
      header:{ //请求头
        "content-type":"application/json"
      },
      success(e){ //回调
          //e为后台返回的值
        console.log(e.data)
      }
    })
  }

 

posted @ 2020-03-11 20:43  Hank·Paul  阅读(2474)  评论(0)    收藏  举报