-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
44 lines (39 loc) · 1.05 KB
/
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
44
;(function () {
const { jQuery: $ } = window
const Tangible = (window.Tangible = window.Tangible || {})
const ajaxConfig = Tangible.ajaxConfig || window.TangibleAjaxConfig || {}
const { url, nonce } = ajaxConfig
function ajax(action, data = {}) {
return new Promise((resolve, reject) => {
$.ajax({
type: 'POST',
url,
data: {
action: 'tangible_ajax_' + action,
nonce,
data,
},
beforeSend: '',
success: '',
error: '',
})
.done(function (res) {
if (res === '1' || res === 1) res = { success: true }
if (typeof res === 'object' && typeof res.success !== 'undefined') {
;(res.success ? resolve : reject)(res.data)
} else {
reject(res)
}
})
.fail(function (res) {
reject({
code: res.status,
message: res.statusText,
})
})
})
}
Tangible.ajax = ajax
// Backward compatibility
Tangible.ajaxConfig = ajaxConfig
})()