-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
66 lines (58 loc) · 1.74 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
function makeAJAXRequest(url, callback, method, postData, dataType){
//This variable will hold the xml request required for AJAX.
var xmlRequest = createXMLHttpRequestObject();
//assign default values to the (method, postData and dataType) arguments.
method = method || 'GET';
postData = postData || null;
dataType = dataType || 'text/plain';
//passes the method to the PHP url we are using.
xmlRequest.open( method, url);
//Sets the content type of the data we are sending to the PHP file.
xmlRequest.setRequestHeader('Content-Type', dataType);
xmlRequest.onreadystatechange = function() {
if (xmlRequest.readyState === 4){
if (xmlRequest.status === 200) {
callback.success(xmlRequest);
} else {
callback.failure(xmlRequest);
}
}
}
//send the AJAX request.
xmlRequest.send(postData);
//returns the AJAX request object;
return xmlRequest;
}
//This function creates the xml request for AJAX.
function createXMLHttpRequestObject(){
var xmlHttp;
//if the browser is IE.
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
//for all other current browsers such as chrome and firefox.
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
//should any problems occur display this message.
if(!xmlHttp){
alert("AJAX request could not be created.");
}else{
return xmlHttp;
}
}
//build a string seperating name from value creating (name, value) pairs.
function encodeVariables(params){
var string = '';
for (i in params) {
string += encodeURIComponent(i) + '=' + encodeURIComponent( params[i]) + '&';
}
return string.slice(0,-1);
}