后台交互
小程序是前端框架,需要和后台交互,本次課程主要介紹網絡API。
小程序提供的網絡訪問API

wx.request接口
發起 HTTPS 網絡請求。
使用rqeust接口前的工作
1.小程序需要到后台設置合法請求域名(一般用於正式發布)

2.關閉開發工具中的url檢測(開發環境設置)
關閉url檢查后可以訪問http://localhost

測試效果

視圖代碼
<!--index.wxml-->
<scroll-view class="container">
<view class='btn'>
<button type='success' bindtap='getwxstore'>獲取微信公眾平台HTML</button>
</view>
<view class='content'>
<textarea value='{{html}}' auto-height maxlength='0' style='width:100%'></textarea>
</view>
</scroll-view>
樣式代碼
/**index.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 20rpx 20rpx;
box-sizing: border-box;
}
.btn{
margin-bottom: 20rpx;
}
.content{
height: 100%;
}
邏輯代碼
//index.js
Page({
data: {
html: ''
},
getwxstore: function() {
var self = this;
wx.request({
url: 'https://mp.weixin.qq.com',
data: {},
header: {
"content-type": "application/json"
},
success:function(res){
console.log(res);
self.setData({
html:res.data
});
}
})
}
})
