问题描述
在封装的过程中,遇到问题
function apiAxios(method, url, params) {
let httpDefault = {
method: method,
baseURL: baseURL,
url: url,
// `params` 是即将与请求一起发送的 URL 参数
// `data` 是作为请求主体被发送的数据
params: method === 'GET' || method === 'DELETE' ? params : null,
data: method === 'POST' || method === 'PUT' ? params : null,
timeout: 10000,
transformRequest: [function (data) {
if (method == 'POST') {
let ret = ''
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
return ret
}
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
// 注意**Promise**使用(Promise首字母大写)
return new Promise((resolve, reject) => {
axios(httpDefault)
// 此处的.then属于axios
.then((res) => {
successState(res)
resolve(res)
}).catch((response) => {
errorState(response)
reject(response)
})
})
}
实现了
不知道原因,求解
application/x-www-form-urlencoded 你请求的类型这个,对http实体来说就是一段你拼的东西,当然你也可以post json格式,但本质还是post了一段字符串