wx.request(OBJECT)
OBJECT參數說明:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
url | String | 是 | 開發者服務器接口地址 |
data | Object、String | 否 | 請求的參數 |
header | Object | 否 | 設置請求的 header , header 中不能設置 Referer |
method | String | 否 | 默認為 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT |
dataType | String | 否 | 默認為 json。如果設置了 dataType 為 json,則會嘗試對響應的數據做一次 JSON.parse |
success | Function | 否 | 收到開發者服務成功返回的回調函數,res = {data: '開發者服務器返回的內容'} |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success返回參數說明:
參數 | 說明 | 最低版本 |
---|---|---|
data | 開發者服務器返回的數據 | |
statusCode | 開發者服務器返回的狀態碼 | |
header | 開發者服務器返回的 HTTP Response Header | 1.2.0 |
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)...)
示例代碼:
wx.request({
url: 'test.php', //僅為示例,並非真實的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json' }, success: function(res) { console.log(res.data) } })
微信小程序開發時,使用公共的url端口,自定義函數
module.exports = {
// API 接口
API_HOST: "url/"
}
const config = require('../config.js');
module.exports = {
//get方式請求
GET: function (url = '', data = {}, fn) {
console.log(data);
wx.request({
url: config.API_HOST + url,//請求地址
method: 'get',//請求方式
data: data,//請求參數
header: { "Content-Type": "application/x-www-form-urlencoded" },
success: function (res) {
fn(res);
}
});
},
//post方式請求
POST: function (url = '', data = {}, fn) {
wx.request({
url: config.API_HOST + url,//請求地址
method: 'post',//請求方式
data: data,//請求參數
header: { "Content-Type":"application/x-www-form-urlencoded"},
success: function (res) {
fn(res);
}
});
},
}
1 // 加載配置文件 2 const config = require('config.js'); 3 var app=getApp(); 4 module.exports = { 5 /** 6 * get方式請求,ulr是請求api號,token是登陸token,不用token就傳空,fn是函數成功的回調函數,data為向后台傳遞的參數by:張濤20180303 7 */ 8 GET: function (url = '',token='' ,data = {}, fn,fail) { 9 wx.request({ 10 url: config.API_HOST + url,//請求地址 11 method: 'get',//請求方式 12 data: data,//請求參數 13 header: { "Content-Type": "application/json" ,'token':token}, 14 success: function (res) { 15 // 判斷token是否失效 16 if (res.data.code=='JWT00002'||res.data.code=='JWT00001'||res.data.code=='JWT00004'||res.data.code=='403') { 17 wx.navigateTo({ 18 url:'/pages/login/login' 19 }) 20 return false; 21 } 22 fn(res); 23 }, 24 fail: function (res) { 25 // wx.showToast({ 26 // title: '請求服務器失敗,請稍后再試!', 27 // icon: 'loading', 28 // duration: 2000 29 // }) 30 } 31 }); 32 }, 33 34 /** 35 * post方式請求 36 */ 37 POST: function (url = '',token='', data = {}, fn ,fail) { 38 wx.request({ 39 url: config.API_HOST + url,//請求地址 40 method: 'post',//請求方式 41 data: data,//請求參數 42 header: { "Content-Type": "application/json",'token':token}, 43 success: function (res) { 44 // 判斷token是否失效 如果失效就跳轉登錄頁面 45 if (res.data.code=='JWT00002'||res.data.code=='JWT00001'||res.data.code=='JWT00004'||res.data.code=='403') { 46 wx.navigateTo({ 47 url:'/pages/login/login' 48 }) 49 return false; 50 } 51 fn(res); 52 }, 53 fail: function (res) { 54 // wx.showToast({ 55 // title: '請求服務器失敗,請稍后再試!', 56 // icon: 'loading', 57 // duration: 2000 58 // }) 59 } 60 }); 61 } 62 63 }
增加個人封裝的小程序請求方法