-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJajax-0.1.0.min.js
9 lines (9 loc) · 1.44 KB
/
Jajax-0.1.0.min.js
1
2
3
4
5
6
7
8
9
/*!
* Jajax JavaScript Library v0.1.0
*
* author: 施哲晨
* inf:一个轻量化的ajax组件
* Date: 2016-03-22
*
*/
var Jajax=function(config){var file=config.file;var url=config.url;var data=config.data;var complete=config.complete;var callback=config.callback;var json_back=config.json_back;var error=config.error;var type=config.type||"post";var form=null;var xhr=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");if(type=="GET"||type=="get"){url+="?";data=(function(obj){var str="";for(var name in obj){str+=name+"="+obj[name]+"&"}return str})(data);url+=data;xhr.open("get",url,true);xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")}else{form=new FormData();for(var filename in file){form.append(filename,file[filename])}for(var name in data){form.append(name,data[name])}}xhr.onreadystatechange=_onreadystatechange;if(type=="get"||type=="GET"){xhr.send()}else{xhr.open("post",url,true);xhr.send(form)}var jsBox=Object.create(null);jsBox.isFunction=function(o){return Object.prototype.toString.apply(o)==="[object Function]"};function _onreadystatechange(){if(xhr.readyState==4){if(xhr.status==200){if(json_back==true){var result=xhr.responseText;if(result){var re=eval("("+result+")")}else{var re=""}complete(re)}else{complete(xhr.responseText)}if(jsBox.isFunction(callback)){callback()}}else{if(jsBox.isFunction(error)){error()}console.log("error:readyState:"+xhr.readyState+"status:"+xhr.status)}}}};