|
|
@@ -64,7 +64,7 @@ request.interceptors.response.use(function (response) {
|
|
|
// ajax 自定义各种数据请求的方法
|
|
|
export default function ajax (config) {
|
|
|
// 1. 先获取到请求的一些必要参数
|
|
|
- const { url = '', method = 'GET', data = {}, headers = {} } = config
|
|
|
+ const { url = '', method = 'GET', data = {}, headers = {}, withCredentials = true } = config
|
|
|
// 2. 判断我们请求的类型是 get 还是 post 还是其他
|
|
|
switch (method.toUpperCase()) {
|
|
|
case 'GET':
|
|
|
@@ -75,10 +75,10 @@ export default function ajax (config) {
|
|
|
for (const key in data) {
|
|
|
obj.append(key, data[key])
|
|
|
}
|
|
|
- return request.post(url, obj, { headers })
|
|
|
+ return request.post(url, obj, { headers, withCredentials })
|
|
|
}
|
|
|
// get 请求的参数我们需要放在 params 中
|
|
|
- return request.get(url, { params: data })
|
|
|
+ return request.get(url, { params: data, withCredentials })
|
|
|
case 'POST':
|
|
|
// post 请求
|
|
|
// 1. 表单提交数据
|
|
|
@@ -88,7 +88,7 @@ export default function ajax (config) {
|
|
|
for (const key in data) {
|
|
|
obj.append(key, data[key])
|
|
|
}
|
|
|
- return request.post(url, obj, { headers })
|
|
|
+ return request.post(url, obj, { headers, withCredentials })
|
|
|
}
|
|
|
// 2. 文件数据
|
|
|
if (headers['content-type'] == 'multipart/form-data') {
|
|
|
@@ -96,21 +96,18 @@ export default function ajax (config) {
|
|
|
for (const key in data) {
|
|
|
obj.append(key, data[key])
|
|
|
}
|
|
|
- return request.post(url, obj, { headers })
|
|
|
+ return request.post(url, obj, { headers, withCredentials })
|
|
|
}
|
|
|
// 3. json 数据
|
|
|
- return request.post(url, data, {headers})
|
|
|
+ return request.post(url, data, {headers, withCredentials})
|
|
|
case 'PUT':
|
|
|
- // 通常用来修改数据用的 --- 数据更新
|
|
|
- return request.put(url, data)
|
|
|
+ return request.put(url, data, { withCredentials })
|
|
|
case 'DELETE':
|
|
|
- // 删除数据
|
|
|
- return request.delete(url, { data })
|
|
|
+ return request.delete(url, { data, withCredentials })
|
|
|
case 'PATCH':
|
|
|
- // 更新局部资源
|
|
|
- return request.patch(url, data)
|
|
|
+ return request.patch(url, data, { data, withCredentials })
|
|
|
default:
|
|
|
- // 如果前面全部都不是
|
|
|
- return request.request(config)
|
|
|
+ // 兜底原样透传全部config
|
|
|
+ return request.request({...config, withCredentials})
|
|
|
}
|
|
|
-}
|
|
|
+}
|