### wx.request(OBJECT)
OBJECT參數說明:

success返回參數說明:

data 數據說明:
最終發送給服務器的數據是 String 類型,如果傳入的 data 不是 String 類型,會被轉換成 String 。轉換規則如下:
對于 header['content-type'] 為 application/json 的數據,會對數據進行 JSON 序列化
對于 header['content-type'] 為 application/x-www-form-urlencoded 的數據,會將數據轉換成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
示例代碼:
//獲取應用實例
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/demo/demo.php?type='+type, //僅為示例,并非真實的接口地址
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res.data);
_self.setData({
"banner":res.data.results,
});
}
})
}
返回值:
## 接口規則:
使用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://*******.jpg",
'content'=>"框架的視圖層"
);
}
$detail = array('error_code'=>200,'results'=>$detail);
echo json_encode($detail);
?>
基礎庫 1.4.0 開始支持,低版本需做兼容處理
返回一個 requestTask 對象,通過 requestTask,可中斷請求任務。
requestTask 對象的方法列表:

示例代碼:
const requestTask = wx.request({
url: 'test.php', //僅為示例,并非真實的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res.data)
}
})
requestTask.abort() // 取消請求任務
Bug & Tip
tip: content-type 默認為 'application/json';
tip: url 中不能有端口;
bug: 開發者工具 0.10.102800 版本,header 的 content-type 設置異常;
使用setData需注意

商品分類功能 示例:
有數據:

無數據:

logs.js
Page({
data: {
navLeftItems: [],
navRightItems: [],
curNav: 1,
curIndex: 0
},
onLoad: function () {
// 加載的使用進行網絡訪問,把需要的數據設置到data數據對象
var that = this
wx.request({
url: 'http://huanqiuxiaozhen.com/wemall/goodstype/typebrandList',
method: 'GET',
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log(res)
that.setData({
navLeftItems: res.data,
navRightItems: res.data
})
}
})
},
//事件處理函數
switchRightTab: function (e) {
console.log(e);
// 獲取item項的id,和數組的下標值
let id = e.target.dataset.id,
index = parseInt(e.target.dataset.index);
// 把點擊到的某一項,設為當前index
this.setData({
curNav: id,
curIndex: index
})
}
})
## 模版調用:
logs.wxml
<!--主盒子-->
<view class="container">
<!--左側欄-->
<view class="nav_left">
<block wx:for="{{navLeftItems}}">
<!--當前項的id等于item項的id,那個就是當前狀態-->
<!--用data-index記錄這個數據在數組的下標位置,使用data-id設置每個item的id值,供打開2級頁面使用-->
<view class="nav_left_items {{curNav == item.id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.id}}">{{item.tree.desc}}</view>
</block>
</view>
<!--右側欄-->
<view class="nav_right">
<!--如果有數據,才遍歷項-->
<view wx:if="{{navRightItems[curIndex].tree.nodes[1].tree.nodes}}">
<block wx:for="{{navRightItems[curIndex].tree.nodes[1].tree.nodes}}">
<view class="nav_right_items">
<navigator url="../list/index?brand={{item.tree.id}}&typeid={{navRightItems[curIndex].id}}">
<!--用view包裹圖片組合,如果有圖片就用,無圖片提供就使用50x30的這個默認圖片-->
<view>
<block wx:if="{{item.tree.logo}}">
<image src="{{item.tree.logo}}"></image>
</block>
<block wx:else>
<image src="http://temp.im/50x30"></image>
</block>
</view>
<!--如果有文字,就用文字;無文字就用其他-->
<view wx:if="{{item.tree.desc}}">
<text>{{item.tree.desc}}</text>
</view>
<view wx:else>
<text>{{item.tree.desc2}}</text>
</view>
</navigator>
</view>
</block>
</view>
<!--如果無數據,則顯示數據-->
<view style='text-align:center' wx:else>暫無數據。。。</view>
</view>
</view>
logs.wxss
page{
background: #f5f5f5;
}
/*總體主盒子*/
.container {
position: relative;
width: 100%;
height: 100%;
background-color: #fff;
color: #939393;
}
/*左側欄主盒子*/
.nav_left{
/*設置行內塊級元素(沒使用定位)*/
display: inline-block;
width: 25%;
height: 100%;
/*主盒子設置背景色為灰色*/
background: #f5f5f5;
text-align: center;
}
/*左側欄list的item*/
.nav_left .nav_left_items{
/*每個高30px*/
height: 30px;
/*垂直居中*/
line-height: 30px;
/*再設上下padding增加高度,總高42px*/
padding: 6px 0;
/*只設下邊線*/
border-bottom: 1px solid #dedede;
/*文字14px*/
font-size: 14px;
}
/*左側欄list的item被選中時*/
.nav_left .nav_left_items.active{
/*背景色變成白色*/
background: #fff;
}
/*右側欄主盒子*/
.nav_right{
/*右側盒子使用了絕對定位*/
position: absolute;
top: 0;
right: 0;
flex: 1;
/*寬度75%,高度占滿,并使用百分比布局*/
width: 75%;
height: 100%;
padding: 10px;
box-sizing: border-box;
background: #fff;
}
/*右側欄list的item*/
.nav_right .nav_right_items{
/*浮動向左*/
float: left;
/*每個item設置寬度是33.33%*/
width: 33.33%;
height: 80px;
text-align: center;
}
.nav_right .nav_right_items image{
/*被圖片設置寬高*/
width: 50px;
height: 30px;
}
.nav_right .nav_right_items text{
/*給text設成塊級元素*/
display: block;
margin-top: 5px;
font-size: 10px;
/*設置文字溢出部分為...*/
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
- 商城api接口
- 首頁數據獲取
- 分類接口
- 購物車接口
- 商品信息接口
- 搜索接口
- 訂單列表接口
- 店鋪接口
- 收藏接口
- 收貨地址接口
- 生成訂單接口
- 支付接口
- 會員中心接口
- 登錄注冊接口
- 關于我們
- 圖片上傳
- 分銷中心
- 分銷明細
- 代金券
- 平臺紅包列表
- 分銷申請列表
- 我的推廣
- 微信小程序
- 簡介
- 開發前準備
- 目錄結構介紹
- 發起請求
- 網絡請求提交表單
- 代碼及開發所遇到問題總結
- 導航跳轉時所遇到的問題
- 緩存數據與數據取得的問題
- 如何引入外部css
- 如何定義與使用全局變量
- 如何定義新的界面
- 微信小程序支付
- 小程序的手機驗證碼登錄
- 上傳,下載
- 提示框
- app.json配置
- 配置demo
- pages
- window
- tabBar
- networkTimeout
- debug
- page.json
- 緩存
- 特效
- 滑動方式
- 城市切換
- 五星好評
- Switch
- 上拉加載
- wxml 標簽
- 視圖容器
- 基礎內容
- 表單組件
- 導航
- 媒體組件
- 自定義提示框
- 小程序內訪問網頁
- 倒計時顯示
- 微信小程序,如何在返回前一個頁面時,執行前一個頁面的方法
- 在本地可以請求到數據,但手機上是請求不到的
- curl請求失敗
- 代碼同步
- 短信平臺更換