微信小程序使用request连接API

一.微信小程序原文档:

发起网络请求说明

OBJECT参数说明:

参数名类型必填默认值说明最低版本
urlString 开发者服务器接口地址 
dataObject/String/ArrayBuffer 请求的参数 
headerObject 设置请求的 header,header 中不能设置 Referer。 
methodStringGET(需大写)有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
dataTypeStringjson如果设为json,会尝试对返回的数据做一次 JSON.parse 
responseTypeStringtext设置响应的数据类型。合法值:text、arraybuffer1.7.0
successFunction 收到开发者服务成功返回的回调函数 
failFunction 接口调用失败的回调函数 
completeFunction 接口调用结束的回调函数(调用成功、失败都会执行) 

success返回参数说明:

参数类型说明最低版本
dataObject/String/ArrayBuffer开发者服务器返回的数据 
statusCodeNumber开发者服务器返回的 HTTP 状态码 
headerObject开发者服务器返回的 HTTP Response Header1.2.0

data 数据说明:

最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:

  • 对于 GET 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
  • 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会对数据进行 JSON 序列化
  • 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)

二.实例:

 onLoad: function (event) {
   wx.request({
     url: 'https://www.apiopen.top/novelApi',
     data: {},
     method: 'GET',
     header:{
       "Content-type":""
     },
     success:function(res){
        console.log(res)
     },
     fail:function(error){
       console.log("false")
     },
     complete:function(){

     }
   })
  }


三.注意:

1.安全证书设置

https://blog.csdn.net/yiye2017zhangmu/article/details/82493341

2."Content-type":"application/json "会报错,最好直接 "Content-type":" "

3.url必须是https://....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值