summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/shared/utils/http.ts
diff options
context:
space:
mode:
authorChuyi Guo <guochuyi@chinamobile.com>2019-08-23 11:45:30 +0000
committerGerrit Code Review <gerrit@onap.org>2019-08-23 11:45:30 +0000
commit5c9f4896c435936f41c54107626de7f700e3a012 (patch)
treeb9fe7b5c0655ecb7c4e85b6b0f77ac365399f013 /usecaseui-portal/src/app/shared/utils/http.ts
parentc048639a46be107578e8ce0d8721bc7b5d4fe791 (diff)
parent1c5c010542dc53872b7213dbf9343aa92869673a (diff)
Merge "feat: add ajax request function"
Diffstat (limited to 'usecaseui-portal/src/app/shared/utils/http.ts')
-rw-r--r--usecaseui-portal/src/app/shared/utils/http.ts63
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)
+ }
+
+ })
+
+ })
+}
+
+