forked from flouthoc/minAjax.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (91 loc) · 3.61 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*|--minAjax.js--|
|--(A Minimalistic Pure JavaScript Header for Ajax POST/GET Request )--|
|--Author : flouthoc (gunnerar7@gmail.com)(http://github.com/flouthoc)--|
|--Contributers : Add Your Name Below--|
*/
function initXMLhttp() {
var xmlhttp;
if (window.XMLHttpRequest) {
//code for IE7,firefox chrome and above
xmlhttp = new XMLHttpRequest();
} else {
//code for Internet Explorer
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function minAjax(config) {
/*Config Structure
url:"reqesting URL"
type:"GET or POST"
method: "(OPTIONAL) True for async and False for Non-async | By default its Async"
debugLog: "(OPTIONAL)To display Debug Logs | By default it is false"
data: "(OPTIONAL) another Nested Object which should contains reqested Properties in form of Object Properties"
success: "(OPTIONAL) Callback function to process after response | function(data,status)"
*/
if (!config.url) {
if (config.debugLog == true)
console.log("No Url!");
return;
}
if (!config.type) {
if (config.debugLog == true)
console.log("No Default type (GET/POST) given!");
return;
}
if (!config.method) {
config.method = true;
}
if (!config.debugLog) {
config.debugLog = false;
}
var xmlhttp = initXMLhttp();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (config.success) {
config.success(xmlhttp.responseText, xmlhttp.readyState);
}
if (config.debugLog == true)
console.log("SuccessResponse");
if (config.debugLog == true)
console.log("Response Data:" + xmlhttp.responseText);
} else {
if (config.debugLog == true)
console.log("FailureResponse --> State:" + xmlhttp.readyState + "Status:" + xmlhttp.status);
}
}
var sendString = [],
sendData = config.data;
if( typeof sendData === "string" ){
var tmpArr = String.prototype.split.call(sendData,'&');
for(var i = 0, j = tmpArr.length; i < j; i++){
var datum = tmpArr[i].split('=');
sendString.push(encodeURIComponent(datum[0]) + "=" + encodeURIComponent(datum[1]));
}
}else if( typeof sendData === 'object' && !( sendData instanceof String || (FormData && sendData instanceof FormData) ) ){
for (var k in sendData) {
var datum = sendData[k];
if( Object.prototype.toString.call(datum) == "[object Array]" ){
for(var i = 0, j = datum.length; i < j; i++) {
sendString.push(encodeURIComponent(k) + "[]=" + encodeURIComponent(datum[i]));
}
}else{
sendString.push(encodeURIComponent(k) + "=" + encodeURIComponent(datum));
}
}
}
sendString = sendString.join('&');
if (config.type == "GET") {
xmlhttp.open("GET", config.url + "?" + sendString, config.method);
xmlhttp.send();
if (config.debugLog == true)
console.log("GET fired at:" + config.url + "?" + sendString);
}
if (config.type == "POST") {
xmlhttp.open("POST", config.url, config.method);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(sendString);
if (config.debugLog == true)
console.log("POST fired at:" + config.url + " || Data:" + sendString);
}
}