-
Notifications
You must be signed in to change notification settings - Fork 11
/
ajax.js
72 lines (64 loc) · 1.72 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
function _ajax() {
this.xmlHttp = new XMLHttpRequest();
this.cancel = function() {
var xmlHttp = this.xmlHttp;
xmlHttp.abort();
xmlHttp.onreadyStateChange = function() {}
}
this.testConn = function(success_callback, error_callback) {
var xmlHttp = this.xmlHttp;
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
var reply = xmlHttp.responseText;
if (reply == "") error_callback();
else success_callback();
}
}
xmlHttp.open('GET', "http://www.google.com", true);
xmlHttp.send(null);
}
this.parseResponse = function(reply, responseType) {
if(responseType == 'text') {
return reply.responseText;
}else if(responseType == 'xml') {
return reply.responseXML;
}else {
return this.parseJSON(reply.responseText);
}
}
this.get = function(rURL, callback) {
var aux = this;
var xmlHttp = this.xmlHttp;
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
response = JSON.parse(xmlHttp.responseText);
if(response == "" || response.error == -1) {
aux.onResponseError(response);
return;
}
callback(response);
}
};
xmlHttp.open('GET', rURL, true);
xmlHttp.send(null);
}
this.getText = function(rURL, callback) {
var aux = this;
var xmlHttp = this.xmlHttp;
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
response = xmlHttp.responseText;
if(response == "") {
aux.onResponseError(response);
return;
}
callback(response);
}
};
xmlHttp.open('GET', rURL, true);
xmlHttp.send(null);
}
this.onResponseError = function(response) {
alert('error response');
}
}