### wx.request(OBJECT)
發起網絡請求。**使用前請先閱讀說明**。
**OBJECT參數說明:**
| 參數名 | 類型 | 必填 | 默認值 | 說明 | 最低版本 |
| ------------ | ------------------------- | ---- | ---- | ---------------------------------------- | ---------------------------------------- |
| url | String | 是 | | 開發者服務器接口地址 | |
| data | Object/String/ArrayBuffer | 否 | | 請求的參數 | |
| header | Object | 否 | | 設置請求的 header,header 中不能設置 Referer。 | |
| method | String | 否 | GET | (需大寫)有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT | |
| dataType | String | 否 | json | 如果設為json,會嘗試對返回的數據做一次 JSON.parse | |
| responseType | String | 否 | text | 設置響應的數據類型。合法值:text、arraybuffer | [1.7.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| success | Function | 否 | | 收到開發者服務成功返回的回調函數 | |
| fail | Function | 否 | | 接口調用失敗的回調函數 | |
| complete | Function | 否 | | 接口調用結束的回調函數(調用成功、失敗都會執行) | |
**success返回參數說明:**
| 參數 | 類型 | 說明 | 最低版本 |
| ---------- | ------------------------- | ------------------------------ | ---------------------------------------- |
| data | Object/String/ArrayBuffer | 開發者服務器返回的數據 | |
| statusCode | Number | 開發者服務器返回的 HTTP 狀態碼 | |
| header | Object | 開發者服務器返回的 HTTP Response Header | [1.2.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
**data 數據說明:**
最終發送給服務器的數據是 String 類型,如果傳入的 data 不是 String 類型,會被轉換成 String 。轉換規則如下:
- 對于 `GET` 方法的數據,會將數據轉換成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
- 對于 `POST` 方法且 `header['content-type']` 為 `application/json` 的數據,會對數據進行 JSON 序列化
- 對于 `POST` 方法且 `header['content-type']` 為 `application/x-www-form-urlencoded` 的數據,會將數據轉換成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
**示例代碼:**
```
wx.request({
url: 'test.php', //僅為示例,并非真實的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json' // 默認值
},
success: function(res) {
console.log(res.data)
}
})
```
**返回值:**
> 基礎庫 1.4.0 開始支持,低版本需做[兼容處理](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html)
返回一個 `requestTask` 對象,通過 `requestTask`,可中斷請求任務。
**requestTask 對象的方法列表:**
| 方法 | 參數 | 說明 | 最低版本 |
| ----- | ---- | ------ | ---------------------------------------- |
| abort | | 中斷請求任務 | [1.4.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
**示例代碼:**
```
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
1. `tip`: content-type 默認為 'application/json';
2. `tip`: url 中不能有端口;
3. `bug`: 開發者工具 `0.10.102800` 版本,`header` 的 `content-type` 設置異常;
- 簡介
- 第一章 公眾號開發
- 使用微信JSSDK
- 接口權限配置
- 分享接口
- 隱藏按鈕項
- 微信支付
- 第二章 小程序開發
- 基礎知識
- 分包加載
- WXSS樣式表
- 配置
- app.json配置
- window
- tabBar
- page.json配置
- 邏輯層
- app.js
- 場景值
- page.js
- 初始化數據
- 生命周期函數
- 頁面相關事件處理函數
- 事件處理函數
- 頁面實例方法
- 路由
- 文件作用域
- 模塊化
- 視圖層
- 模板語法
- 列表渲染
- 條件渲染
- 模板
- 事件
- 引用
- WXS語法規范
- WXS數據類型
- WXS控制流程
- WXS基礎類庫
- 組件
- 視圖容器
- view
- scroll-view
- swiper
- movable-view
- cover-view
- 基礎組件
- icon
- text
- rich-text
- progress
- 表單組件
- button
- checkbox
- form
- input
- label
- picker
- picker-view
- radio
- slider
- switch
- textarea
- 導航組件
- navigator
- 媒體組件
- audio
- image
- video
- camera
- 地圖組件
- map
- 畫布組件
- canvas
- 開放能力
- web-view
- 自定義組件
- 組件模版和樣式
- Component
- 組件傳值
- 組件事件
- Behaviors
- 組件間關系
- 網絡請求
- wx.request
- 微信登錄
- 獲取 openid 和 unionid
- 獲取用戶信息
- 將 wx.request 封裝為 promise
- 上傳圖片接口封裝
- 數據存儲
- 存儲數據和讀取數據
- 獲取數據緩存信息
- 移除數據緩存
- 獲取用戶設置
- openSetting
- getSetting
- 第三章 小游戲開發
- 參考資料