-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathajax.js
79 lines (74 loc) · 2.5 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
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
// fetch the contents from the URL "url". Once successful, apply the function
// "cont()" to the contents. If unsuccessful, apply the function err(msg).
function read_from_url(url, cont, err, mimetype) {
var undefined;
let req;
if (mimetype == undefined) mimetype = 'text/xml';
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
req = new XMLHttpRequest();
if (req.overrideMimeType)
req.overrideMimeType(mimetype);
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
cont(req.responseText)
} else {
err('Could not read from ' + url + ': Error ' + req.status);
}
}
}
if (!url.match(/^https?:/)) {
var prefix = location.href;
url = prefix.replace(/\/[^\/]*$/, '/') + url;
}
req.open("GET", url, true);
req.send(null);
}
function post_to_url(url, params, cont, err, mimetype) {
var undefined;
let req;
if (mimetype == undefined) mimetype = 'text/xml';
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
req = new XMLHttpRequest();
if (req.overrideMimeType)
req.overrideMimeType(mimetype);
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
cont(req.responseText)
} else {
err('Could not read from ' + url + ': Error ' + req.status);
}
}
}
if (!url.match(/^https?:/)) {
var prefix = location.href;
url = prefix.replace(/\/[^\/]*$/, '/') + url;
}
var data = new FormData();
for (var prop in params) {
data.append(prop, params[prop]);
}
req.open("POST", url, true);
req.send(data);
}
// fetch the contents from the URL "url" into the DOM node with id attribute
// "id". Once successful, apply the optionally provided function "cont()".
function fetch_content(id, url, cont) {
const node = document.getElementById(id);
read_from_url(url,
function(responseText) {
node.innerHTML = responseText;
var undefined;
if (cont != undefined) cont();
},
function(errmsg) {
node.innerHTML = 'Could not read from ' + url + ': ' + errmsg
});
}