提交 86a69d99 作者: 翁进城

request增加配置参数

上级 d487663a
...@@ -5,10 +5,10 @@ import config from './config'; ...@@ -5,10 +5,10 @@ import config from './config';
* @param url 请求url * @param url 请求url
* @param method 请求方法类型 * @param method 请求方法类型
* @param data 请求的参数 * @param data 请求的参数
* @param options 额外参数
* @returns Promise<Response> * @returns Promise<Response>
*/ */
export default function request(url: string, method: String = 'get', data?: any): Promise<Response<any>> { export default function request(url: string, method: String = 'get', data?: any, options = {}): Promise<Response<any>> {
let options = {};
switch (method.toLowerCase()) { switch (method.toLowerCase()) {
case 'get': case 'get':
...@@ -24,6 +24,7 @@ export default function request(url: string, method: String = 'get', data?: any) ...@@ -24,6 +24,7 @@ export default function request(url: string, method: String = 'get', data?: any)
case 'post': case 'post':
options = { options = {
...options,
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
...@@ -48,6 +49,19 @@ export default function request(url: string, method: String = 'get', data?: any) ...@@ -48,6 +49,19 @@ export default function request(url: string, method: String = 'get', data?: any)
}) })
.then((data) => { .then((data) => {
return data; return data;
})
.catch(error => {
if (error.name === 'AbortError') {
console.log('请求已中断');
console.log(error);
} else {
console.error('请求出错', error);
}
return {
code: '-1',
message: '请求失败',
result: null
}
}); });
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论