-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (75 loc) · 3.18 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
/*
SpaceCat
Author: Ryan Pearson
Version: 0.1
Description: An object based pure js ajax request script
based on minAjax.js by flouthoc:
http://github.com/flouthoc/minAjax.js
*/
// Defines the SpaceCat constructor
var SpaceCat = function(){};
// Config
SpaceCat.prototype.config = {};
// adds ajax medthod to SpaceCat.prototype
SpaceCat.prototype.ajax = function(){
// variable declarations (prevents mutiple calls to var definition)
var xmlhttp, sendData, datum,
config = this.config, // prevents users error issues with not including a config
sendString = [],
tmpArr = [];
/*
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)"
*/
// User error handling
config.url || config.debugLog == true && console.log("No Url!");
config.type || config.debugLog == true && console.log("No Default type (GET/POST) given!");
config.method |= true;
config.debugLog |= false;
// xmlhttp
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200) {
config.success && config.success(this.responseText, this.readyState);
config.debugLog == true && console.log("SuccessResponse \nResponse Data:" + this.responseText);
} else {
config.debugLog == true && console.log("FailureResponse --> State:" + this.readyState + "Status:" + this.status);
}
}
sendData = config.data;
if( typeof sendData === "string" ){
tmpArr = String.prototype.split.call(sendData,'&');
for(var i = 0, j = tmpArr.length; i < j; i++){
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) {
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();
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);
config.debugLog == true && console.log("POST fired at:" + config.url + " || Data:" + sendString);
}
};