首页 > Web开发 > 详细

vue--http请求的封装--session

时间:2018-05-25 12:51:34      阅读:277      评论:0      收藏:0      [点我收藏+]
export function Fecth (url, data, file, _method) {
if (file) {
// 需要上传文件
return new Promise((resolve, reject) => {
axios({
url: url,
data: data,
headers: {
‘Content-Type‘: ‘multipart/form-data‘
},
method: ‘POST‘,
withCredentials: true
}).then(response => {
resolve(response)
})
.catch(error => {
reject(error)
})
})
} else {
// 不需要上传文件
return new Promise((resolve, reject) => {
axios({
url: url,
data: data,
transformRequest: [function (data) {
let ret = ‘‘
for (let it in data) {
if ((typeof data[it]) === ‘object‘) {
ret += encodeURIComponent(it) + ‘=‘ + encodeURIComponent(JSON.stringify(data[it])) + ‘&‘
} else {
ret += encodeURIComponent(it) + ‘=‘ + encodeURIComponent(data[it]) + ‘&‘
}
}
return ret
// let ret = qs.stringify(data)
// return ret
}],
headers: {
‘Content-Type‘: ‘application/x-www-form-urlencoded‘
},
method: _method || ‘POST‘
}).then(response => {
resolve(response)
}
)
.catch(error => {
reject(error)
}
)
})
}
}

vue--http请求的封装--session

原文:https://www.cnblogs.com/langqq/p/9087457.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!