## wx.request(OBJECT)
>[danger]wx.request發起的是 HTTPS 請求。
### index.js
~~~
//獲取應用實例
var app = getApp()
Page({
data: {
indicatorDots:true,
autoplay:true,
interval:5000,
duration:1000,
banner: []
},
onLoad: function () {
var that = this
//調用應用實例的方法獲取全局數據
reda(this,'banner');
}
})
function reda(_self,type){
wx.request({
url: 'http://127.0.0.1/miniApp/my_app.php?type='+type, //僅為示例,并非真實的接口地址
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res.data);
_self.setData({
"banner":res.data.results,
});
}
})
}
~~~
>訪問的網絡接口:
如果是有 `AppID` 進行開發的必須配置網絡服務器,同時接口地址請求必須是 `HTTPS` 請求,
本地測試如示例可以使用 `http://127.0.0.1/demo/API`
### 接口規則:
使用PHP來傳送數據,轉換為json數據:
~~~
<?php
if(!isset($_GET['id']) || empty($_GET['id'])) {
echo jsonFormat(array('error_code'=>300,'info'=>'請求失敗'));
}
if($_GET['id']=='1') {
$detail = array(
'id'=>'1',
'title'=>"詳情1",
'img'=>"http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg",
'content'=>"框架的視圖層"
);
}
$detail = array('error_code'=>200,'results'=>$detail);
echo json_encode($detail);
?>
~~~
### 模版調用:
示例中將 `{{banner}}` 映射為 `item`,來分別使用接口傳送的數據
~~~
<view class="container">
<view class="page">
<view class="section section_gap" wx:for="{{banner}}" wx:for-item="item">
<navigator url="{{item.url}}" class="img">
<view class="section__ctn">
<image class="section_img" src="{{item.img_path}}"></image>
</view>
<view class="section__title">{{item.title}}</view>
</navigator>
</view>
</view>
</view>
~~~