-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.js
99 lines (94 loc) · 3.35 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
util = {
get: function(p) {
$.ajax({
url: p.url,
data: JSON.stringify(p.data),
contentType: "application/json",
type: "GET",
success: function(data, textStatus, jqXHR) {
p.callback(data);
},
error: function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status == "403") {
alert(jqXHR.responseJSON.err_message);
window.location = "/logout"
} else {
p.callback({
success: false,
textStatus: textStatus,
error: jqXHR.status,
data: jqXHR.responseJSON
});
}
}
});
},
post: function(p) {
$.ajax({
url: p.url,
type: "POST",
data: JSON.stringify(p.data),
contentType: "application/json",
success: function(data, textStatus, jqXHR) {
console.log({ data, textStatus, jqXHR })
if (p.callback) {
p.callback({
success: true,
textStatus: textStatus,
data: data
})
} else if (p.success) {
p.success(data);
} else {
console.log({ url: p.url, status: "success", data: data });
}
},
error: function(jqXHR, textStatus, errorThrown) {
let err = {
url: p.url,
status: jqXHR.status,
message: (jqXHR.responseJSON === undefined) ? jqXHR.statusText : jqXHR.responseJSON.err_message,
};
console.log({ jqXHR, textStatus, errorThrown })
if (jqXHR.status == "403") {
alert(jqXHR.responseJSON.err_message);
window.location = "/logout"
} else if (p.callback) {
p.callback({
success: false,
textStatus: textStatus,
error: jqXHR.status,
data: jqXHR.responseJSON
});
} else if (p.error) {
p.error(err);
} else if (p.nopop) {
console.log(err);
} else {
alert("url : " + p.url + "\nstatus : " + err.status + "\nmessage : " + err.message);
}
}
});
},
getCookie: function(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
},
setCookie: function(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
}