-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
36 lines (34 loc) · 927 Bytes
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var util = {
/**
* 基于fetch的get方法
* @method post
* @param {string} url
* @param {function} callback 请求成功回调
*/
ajax:function(params){
var url = params.url,
postData = params.data,
method = params.method || 'get',
headers = params.headers,
success = params.success || function(){},
failure = params.failure || function(){};
var opts = {
method:method,
headers:{
'Accept':'application/json',
'Content-Type':'application/json'
}
};
method=='post' ? opts.body=JSON.stringify(postData) : null;
opts.headers = Object.assign(opts.headers,headers);
fetch(url,opts)
.then((response) => response.text())
.then((responseText) => {
success(JSON.parse(responseText));
})
.catch(function(err){
failure(err);
});
},
}
module.exports = util;