微信小程序网络请求wx.request详解及实例
如果说小程序api里面最重要一个接口是哪一个?那么首推wx.request()。相当于在小程序内请发起一个https请求(本地调试模式下支持http)。http协议中共定义了八种方法或者叫“动作”来表明对request-uri指定的资源的不同操作方式。
小程序对以上8种方法全部支持。不过对于网络资源主要还是增删改查四种类型就够了,小程序后台接口主要使用还是类似restfull的方式。
put请求string
wx.request({ url: 'https://a86.cn/chishenme/chishenmedeciper', method:'put', datatype:'string', data:'this is strng data 这是一个中文数据', header: {'content-type': 'application/json'}, success:function(res){ console.log('this is put request result'+ res.data) } })
post请求 string header 不同
wx.request({ url: 'https://a86.cn/chishenme/chishenmedeciper', method: 'post', datatype: 'string', data:'this is post string data', header: {'content-type':'application/x-www-form-urlencoded'}, success: function (res) { console.log('this is post request result' + res.data) } })
注意点
1、请求的方法类型必须大写如get、post、put;
2、请求的方法类型需要用单引号或双引号包括起来;
3、datatye 需要用单引号或双引号包括起来(内部约定也用大写);
4、content-type 默认为 'application/json' 对于习惯用request.getparameter()获取参数的同学要注意
5、如果使用默认的header,服务端需要对传送过来的内容“流转字符串”
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
如对本文有疑问, 点击进行留言回复!!
【第138期】 游戏策划:应聘前HR要求把游戏玩到30级,怎么玩?
网友评论