一个原生js操作Ajax封装
来源:互联网 发布:sqlplus导出sql脚本 编辑:程序博客网 时间:2024/05/03 21:48
/*------------------------------------------ * @ ajax类------------------------------------------*/M.ajax.prototype = {init : function (config) {var that = this;that.type = config.type ? config.type.toUpperCase() : "GET";that.dataType = config.dataType || "html";that.success = config.success || false;that.error = config.error || false;that.url = config.url || "";that.data = config.data || "";that.contentType = config.contentType ||"application/x-www-form-urlencoded";that.cache = config.cache === false ? false : true;that.request = that.getRequest();if(M.typeOf( that.data ) == "object"){var tmp = '';for(var k in that.data){tmp+='&'+k+'='+that.data[k];}if(tmp)that.data = tmp.replace("&","");tmp = null;}if (that.type == "GET") {var lj = (that.url.indexOf("?") > -1) ? "&" : "?";that.url += that.data ? (lj + that.data) : "";}if (!that.cache) {var now = new Date();now = now.getTime();var lj = (that.url.indexOf("?") > -1) ? "&" : "?";that.url += lj + '_=' + now;}if (that.request) {var req = that.request;req.onreadystatechange = that.bindFunction(that.stateChange, that);req.open( that.type , that.url, true);if (that.type == "POST"){req.setRequestHeader("Content-type", that.contentType);req.send(that.data);}else{req.send(null);}}},getRequest : function () {if (window.ActiveXObject)return new ActiveXObject('Microsoft.XMLHTTP');else if (window.XMLHttpRequest)return new XMLHttpRequest();return false;},bindFunction : function (caller, object) {return function () {return caller.apply(object, [object]);};},stateChange : function (object) {that = this;if (that.request.readyState == 4) {if (that.request.status == 200) {if (that.dataType == "json") {try {if (typeof JSON === "object") {var json = JSON.parse(that.request.responseText);} else {var json = eval('(' + that.request.responseText + ')');}} catch (e) {if (that.error) {that.error();return false;}}if (typeof json === "object") {if (that.success) {that.success(json,that.request.status,that.request);return true;}} else {if (that.error) {that.error();return false;}}} else {if (that.success) {that.success(that.request.responseText,that.request.status,that.request);return true;}}} else {if (that.error) {that.error(that.request);return false;}}}}}M.ajax.prototype.init.prototype = M.ajax.prototype;
版权所有:www.meilele.com
0 0
- 一个原生js操作Ajax封装
- 原生js封装Ajax
- 原生js封装ajax
- 原生JS封装Ajax
- 原生JS封装ajax()
- 自己做工具--用原生js封装一个AJAX插件
- 将ajax请求封装为一个对象(原生js)
- 原生js ajax 操作
- 原生JS Ajax操作
- 原生JS Ajax操作
- 原生JS Ajax操作
- 原生JS Ajax操作
- 原生js Ajax操作
- 原生js封装ajax方法
- 原生JS封装AJAX请求
- 原生js封装ajax函数
- 原生JS封装ajax方法
- js 原生ajax:封装函数
- IOS7适配之:返回按钮偏移问题
- 构造方法重载和调用
- ubuntu下忘记mysql root 密码
- svn update老报错
- Win7下硬盘安装Ubuntu12.04双系统
- 一个原生js操作Ajax封装
- CLBLAS build
- 看代码猜古诗哈哈逗死我了
- NoSQL数据库探讨之 —— 为什么要用非关系数据库?
- DeveXpress ASPxGridView获取要批量删除或者批量更改的Id(AspxGridView复选框的全选|反选功能)
- trac 做代码比较时“Too many open files”
- <script>中text/javascript和application/javascript两个不同属性的区别
- APK安装过程及原理详解
- 字符编码的奥秘