diff options
author | cyuamber <xuranyjy@chinamobile.com> | 2019-08-23 17:55:21 +0800 |
---|---|---|
committer | cyuamber <xuranyjy@chinamobile.com> | 2019-08-23 17:55:31 +0800 |
commit | 1c5c010542dc53872b7213dbf9343aa92869673a (patch) | |
tree | 23539dcacd907187fd75837e06a84121549a7ff2 /usecaseui-portal/src/app/shared/utils | |
parent | 1ed096510209590d1656489a74692eb5ebcdeee4 (diff) |
feat: add ajax request function
Change-Id: Ib52fbe8c39f5d1e37dc4880c36f5a68171530d38
Issue-ID: USECASEUI-307
Signed-off-by: cyuamber <xuranyjy@chinamobile.com>
Diffstat (limited to 'usecaseui-portal/src/app/shared/utils')
-rw-r--r-- | usecaseui-portal/src/app/shared/utils/http.ts | 63 |
1 files changed, 63 insertions, 0 deletions
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(<string>query){
+ query = JSON.parse((<string>query));
+ }
+ for(let key in <object>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)
+ }
+
+ })
+
+ })
+}
+
+
|