小程序08 小程序訪問服務器API


后台交互

小程序是前端框架,需要和后台交互,本次課程主要介紹網絡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
        });
      }
    })
  }
})

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM