WeChatアプレットwx.request
RequestTask wx.request(Object object)
HTTPSネットワーク要求を開始します。
サンプルコード
wx.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})
wx.request
二次包装なしでそれを行うことは本当に簡単ではありません。ここで私のwx.request
包装を共有してください
api.js
const app = getApp()
const request = (url, options) => {
return new Promise((resolve, reject) => {
wx.request({
url: `${app.globalData.host}${url}`,
method: options.method,
data: options.data,
header: {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': app.globalData.accessToken
},
timeout: 10000,
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else if (request.statusCode === 401) {
wx.navigateTo({
url: '/pages/login/login'
})
reject(request.data)
} else if (request.statusCode === 500) {
wx.showModal({
title: '系统错误',
content: request.data.errmsg,
})
reject(request.data)
} else {
reject(request.data)
}
},
fail(error) {
wx.showModal({
title: '系统错误',
content: '服务器正在开小差',
})
reject(error.data)
},
complete: function (res) {}
})
})
}
// 把需要Loading的方法进行封装,减少不必要代码
const requestWithLoading = (url, options) => {
return new Promise((resolve, reject) => {
wx.showLoading({
title: '加载中',
})
wx.request({
url: `${app.globalData.host}${url}`,
method: options.method,
data: options.data,
header: {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': app.globalData.accessToken
},
timeout: 10000,
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else if (request.statusCode === 401) {
wx.navigateTo({
url: '/pages/login/login'
})
reject(request.data)
} else if (request.statusCode === 500) {
wx.showModal({
title: '系统错误',
content: request.data.errmsg,
})
reject(request.data)
} else {
reject(request.data)
}
},
fail(error) {
wx.showModal({
title: '系统错误',
content: '服务器正在开小差',
})
reject(error.data)
},
complete: function (res) {
wx.hideLoading()
}
})
})
}
const get = (url, options = {}) => {
return request(url, {
method: 'GET',
data: options
})
}
const getWithLoading = (url, options = {}) => {
return requestWithLoading(url, {
method: 'GET',
data: options
})
}
const post = (url, options) => {
return request(url, {
method: 'POST',
data: options
})
}
const postWithLoading = (url, options) => {
return requestWithLoading(url, {
method: 'POST',
data: options
})
}
const put = (url, options) => {
return request(url, {
method: 'PUT',
data: options
})
}
const putWithLoading = (url, options) => {
return requestWithLoading(url, {
method: 'PUT',
data: options
})
}
// 不能声明DELETE(关键字)
const remove = (url, options) => {
return request(url, {
method: 'DELETE',
data: options
})
}
const removeWithLoading = (url, options) => {
return requestWithLoading(url, {
method: 'DELETE',
data: options
})
}
module.exports = {
get,
getWithLoading,
post,
postWithLoading,
put,
putWithLoading,
remove,
removeWithLoading
}
使い方
const api = require('../../api/api')
Page()前引入
api.post(login, {
data: ''
}).then(res => {
if(){}
}).catch(err => {
wx.showToast({
title: err.message,
icon: 'none'
})
})
データパラメータの説明
サーバーに送信される最終的なデータはString型です。受信データがString型でない場合は、Stringに変換されます。変換規則は次のとおりです。
GETメソッドのデータの場合、データはクエリ文字列に変換されます(encodeURIComponent(k)= encodeURIComponent(v)&encodeURIComponent(k)= encodeURIComponent(v)...)
POSTメソッドとヘッダー['content-type']はapplication / jsonデータ、データはJSONシリアル化されます
。POSTメソッドとヘッダー['content-type']がapplication / x-www-form-urlencoded dataの場合、データはクエリ文字列に変換されます(encodeURIComponent(k)= encodeURIComponent(v)&encodeURIComponent(k)= encodeURIComponent(v)...)