-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjaxRequest.ts
More file actions
57 lines (55 loc) · 1.43 KB
/
AjaxRequest.ts
File metadata and controls
57 lines (55 loc) · 1.43 KB
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
/// <reference path="typings.d.ts"/>
import * as $ from "jquery";
import Router from "./Router";
export function AjaxRequest(settings: AjaxSettings ){
const newSettings: JQueryAjaxSettings = {};
for (const key in settings){
if (key !== "success" && key !== "error"){
newSettings[key] = settings[key];
}
}
if (
typeof newSettings.url === "string" &&
newSettings.url.substr(0, 5) !== "http:" &&
newSettings.url.substr(0, 6) !== "https:" &&
newSettings.url.substr(0, 1) !== "/"
){
newSettings.url = Router.url(newSettings.url);
}
if (typeof newSettings.data === "object" && typeof newSettings.data.ajax === "undefined"){
newSettings.data.ajax = 1;
}
newSettings.success = (data: AjaxResponse, textStatus, JqXHR) => {
if (data.status){
if (settings.hasOwnProperty("success")){
settings.success(data, textStatus, JqXHR);
}
}else{
if (data.hasOwnProperty("error")){
data.error.forEach((error) => {
if (settings.hasOwnProperty("error")){
settings.error(error, JqXHR);
}
});
}else{
if (settings.hasOwnProperty("error")){
const error: AjaxError = {
error: "unknown",
type: "fatal",
};
settings.error(error, JqXHR);
}
}
}
};
newSettings.error = (JqXHR, textStatus) => {
if (settings.hasOwnProperty("error")){
const error: AjaxError = {
error: textStatus,
type: "fatal",
};
settings.error(error, JqXHR);
}
};
return $.ajax(newSettings);
}