forked from MrTreasure/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
43 lines (41 loc) · 963 Bytes
/
ajax.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
37
38
39
40
41
42
43
const request = (method, url, data, config = {}) => {
const options = Object.assign({}, config, {
url,
method,
data
})
options.headers = options.headers || {}
return new Promise((resolve, reject) => {
window.axios.request(options)
.then(res => {
if (res) {
resolve(res.data)
}
}).catch(res => {
reject(res)
})
})
}
export const ajax = {
get(url, config) {
return request('get', url, null, config)
},
delete(url, config) {
return request('delete', url, null, config)
},
head(url, config) {
return request('head', url, null, config)
},
post(url, data, config) {
return request('post', url, data, config)
},
put(url, data, config) {
return request('put', url, data, config)
},
patch(url, data, config) {
return request('path', url, data, config)
}
}
export const formatURL = (url, name, val) => {
return url.replace(`{${name}}`, val)
}