微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

微信小程序的ajax数据请求wx.request介绍

微信小程序的ajax数据请求wx.request介绍,微信小程序的ajax数据请求,很多同学找不到api在哪个位置,这里单独把小程序的ajax请求给列出来,微信小程序的请求就是wx.request这个api,wx.request(一些对象参数),微信小程序不同于浏览器的ajax请求,可以直接跨域请求不用考虑跨域问题。使用小程序官方
Box-sizing: border-Box;"/>

提供的数据请求api发起数据请求方法

wx.request发起的是https请求。一个微信小程序,同时只能有5个网络请求连接。

OBJECT参数说明:

Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;" class="firstRow">Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">
Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px rgb(221, 221, 221); border-left: 0px; background: 0px 0px rgb(249, 249, 249); vertical-align: baseline; text-align: center;">参数名Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px rgb(221, 221, 221); border-left: 0px; background: 0px 0px rgb(249, 249, 249); vertical-align: baseline; text-align: center;">类型Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px rgb(221, 221, 221); border-left: 0px; background: 0px 0px rgb(249, 249, 249); vertical-align: baseline; text-align: center;">必填Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px rgb(221, 221, 221); border-left: 0px; background: 0px 0px rgb(249, 249, 249); vertical-align: baseline; text-align: center;">说明
Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">urlBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">StringBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">是Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">开发者服务器接口地址Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">dataBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">Object、StringBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">否Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">请求的参数Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">headerBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">ObjectBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">否Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">设置请求的 header , header 中不能设置 RefererBox-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">methodBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">StringBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">否Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">认为 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECTBox-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">successBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">FunctionBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">否Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">收到开发者服务成功返回的回调函数,res = {data: '开发者服务器返回的内容'}Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">failBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">FunctionBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">否Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">接口调用失败的回调函数Box-sizing: border-Box; margin: 0px; padding: 0px; outline: 0px; border: 0px; background: 0px 0px; vertical-align: baseline;">Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">completeBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">FunctionBox-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">否Box-sizing: border-Box; margin: 0px; padding: 6px 8px; outline: 0px; border-top: 0px; border-left: 0px; background: 0px 0px; vertical-align: baseline;">接口调用结束的回调函数调用成功、失败都会执行)示例代码

wx.request(
{url: 'test.PHP',data: 
{
 x: '' , y: ''
},
header: {
'Content-Type': 'application/json'
}, 
 success: function(res) 
 {
 console.log(res.data)
}
}
)

微信小程序中使用fetch做ajax请求

fetch是一种新的ajax请求规范,经懒人建站测试,fetch在小程序中也是支持的,测试ajax请求代码如下:
Box-sizing: border-Box;"/>then中带代码是测试,这里是节选了小部分代码,实际使用需要自行修改

fetch('http://www.jb51.cc/json.PHP?typeid=34&page=1&pagesize=10')
.then(function(response)
{ 
 if(response.status==200){
 that.data.page++;
 return response.json();
}
})
.then(function(data)
{
console.log(data);//更新数据
that.setData({ 
listArr:that.data.page==1 ? data : that.data.listArr.concat(data)
})
console.log(that.data.listArr);
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。