From 1c5c010542dc53872b7213dbf9343aa92869673a Mon Sep 17 00:00:00 2001 From: cyuamber Date: Fri, 23 Aug 2019 17:55:21 +0800 Subject: feat: add ajax request function Change-Id: Ib52fbe8c39f5d1e37dc4880c36f5a68171530d38 Issue-ID: USECASEUI-307 Signed-off-by: cyuamber --- usecaseui-portal/src/app/shared/utils/http.ts | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 usecaseui-portal/src/app/shared/utils/http.ts (limited to 'usecaseui-portal/src/app/shared') diff --git a/usecaseui-portal/src/app/shared/utils/http.ts b/usecaseui-portal/src/app/shared/utils/http.ts new file mode 100644 index 00000000..f68ea5e5 --- /dev/null +++ b/usecaseui-portal/src/app/shared/utils/http.ts @@ -0,0 +1,63 @@ +import axios from 'axios'; + + +export default function http (url:string,data:object = {},method:any = 'get',query?:string | object):any { + return new Promise((resolve,reject) => { + method = method.trim().toLocaleLowerCase() + let promise:any; + if(method === 'get' || method === 'delete'){ + // 若无参数则不传递data + let options:object; + if(JSON.stringify(data) === '{}'){ + options = { method, url }; + }else{ + options = { method, url, params: data}; + } + + // 若请求方式为delete请求,则携带请求头 + if(method === 'delete'){ + + } + + promise = axios(options); + + }else if (method === 'post' || method === 'put') { + if(method === 'post' && query){ + let params:string; + if(query){ + query = JSON.parse((query)); + } + for(let key in query){ + params = '&' + key + '=' + query[key]; + } + params = params.slice(1); + url += '?' + params; + } + promise = axios({ + method, + url, + data, + }) + } + promise + .then((response) => { + // 请求成功返回携带成功状态及响应数据的promise对象 + if(response.status === 200 || 304){ + resolve(response.data) + }else{ + reject(response) + } + }) + .catch((error) => { + if(error.status === 404){ + reject('请求资源不存在') + }else{ + reject(error.message) + } + + }) + + }) +} + + -- cgit 1.2.3-korg