Ajax get和Post的纯js实现代码
2014-07-07 13:34
393 查看
<span style="font-size:18px;">//AJAX类 function AjaxClass() { var XmlHttp = false; try { XmlHttp = new XMLHttpRequest(); //FireFox专有 } catch(e) { try { XmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); } catch(e2) { try { XmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e3) { alert("你的浏览器不支持XMLHTTP对象,请升级到IE6以上版本!"); XmlHttp = false; } } } var me = this; this.Method = "POST"; this.Url = ""; this.Async = true; this.Arg = ""; this.CallBack = function(){}; this.Loading = function(){}; this.Send = function() { if (this.Url=="") { return false; } if (!XmlHttp) { return IframePost(); } XmlHttp.open (this.Method, this.Url, this.Async); if (this.Method=="POST") { XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); } XmlHttp.onreadystatechange = function() { if (XmlHttp.readyState==4) { var Result = false; if (XmlHttp.status==200) { Result = XmlHttp.responseText; } XmlHttp = null; me.CallBack(Result); } else { me.Loading(); } } if (this.Method=="POST") { XmlHttp.send(this.Arg); } else { XmlHttp.send(null); } } //Iframe方式提交 function IframePost() { var Num = 0; var obj = document.createElement("iframe"); obj.attachEvent("onload",function(){ me.CallBack(obj.contentWindow.document.body.innerHTML); obj.removeNode() }); obj.attachEvent("onreadystatechange",function(){ if (Num>=5) {alert(false);obj.removeNode()} }); obj.src = me.Url; obj.style.display = 'none'; document.body.appendChild(obj); } } /*----------------------------调用方法------------------------------ var Ajax = new AjaxClass(); // 创建AJAX对象 Ajax.Method = "POST"; // 设置请求方式为POST Ajax.Url = "default.asp" // URL为default.asp Ajax.Async = true; // 是否异步 Ajax.Arg = "a=1&b=2"; // POST的参数 Ajax.Loading = function(){ //等待函数 document.write("loading..."); } Ajax.CallBack = function(str) // 回调函数 { document.write(str); } Ajax.Send(); // 发送请求 ----------------------------------------------------------- var Ajax = new AjaxClass(); // 创建AJAX对象 Ajax.Method = "GET"; // 设置请求方式为POST Ajax.Url = "default.asp?a=1&b=2" // URL为default.asp Ajax.Async = true; // 是否异步 Ajax.Loading = function(){ //等待函数 document.write("loading..."); } Ajax.CallBack = function(str) // 回调函数 { document.write(str); } Ajax.Send(); // 发送请求 --------------------------------------------------------------------*/ </span>
相关文章推荐
- 原生JS实现Ajax的GET POST请求
- 原生 JS Ajax,GET和POST 请求实例代码
- JS基础【原生JS实现的AJAX发送GET、POST】
- 原生 JS Ajax,GET和POST 请求实例代码
- jquery实现读取页面load get post ajax的几种代码方法
- JS实现Ajax,Josn数据的序列化和反序列化---例: 省市区联动(包含get,post)
- [代码]JS原生Ajax,GET和POST
- AJAX学习 自己动手写ajax登录验证及首页数据绑定(代码片段,主要是登录js,ajax的get和post请求)
- js调用AJAX时Get和post的乱码解决方法
- 不用框架使用ajax 纯js使用ajax post,get范例及其区别
- 使用$.getJSON实现跨域ajax请求示例代码
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- js/ajax跨越访问-jsonp的原理和实例(javascript和jquery实现代码)
- MicrosoftAjax.Function.js 中用call 和apply方法实现回调和委托的代码
- js笔记 - ajax中的get和post说明
- ajax 续3 比较好的对ajax的get和post进行封装和实现回调
- js 11行代码实现ajax
- php 模拟POST|GET操作实现代码
- 不用框架使用ajax 纯js使用ajax post,get范例及其区别
- js笔记 - ajax中的get和post说明