如何将封装好的[JavaScript][AJAX]类改写为高效的长尾词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计574个文字,预计阅读时间需要3分钟。
javascript(function(OOP封装) { // 常用类型定义 var ClassType=['Abstruct'];
// 构造函数 function MyAJAX() { this.msg=undefined; this.xmlHttpInstance=undefined; }
// 构造函数的原型方法 MyAJAX.prototype.build=function() { // 创建一个xmlHttpRequest对象 var xmlHttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlHttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlHttp=new ActiveXObject(Microsoft.XMLHTTP); } return xmlHttp; };
// 导出MyAJAX类 window.MyAJAX=MyAJAX;})(OOP封装);
(OOP封装)AJAXScript.js/*通用发送模型*/ function MyAJAX(){ var ClassType=['Abstruct']; this.msg = undefined; this.xmlHttpInstance = undefined; } MyAJAX.prototype.build = function () { //创建一个xmlHttpRequest var xmlHttp; if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttp; }; MyAJAX.prototype.sendRequest = function (how2Send,xmlHttp,url) { //根据既定方法发送请求 xmlHttp.open(how2Send,url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); }; MyAJAX.prototype.post = function (url,sendMsg) { //用post方法发送请求 var xmlHttp = this.build(); var instanceType = this; this.sendRequest("POST",xmlHttp,url); xmlHttp.send(sendMsg); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState===4 && xmlHttp.status===200){ instanceType.accessedCallback(xmlHttp); }else{ instanceType.failedCallback(xmlHttp); } } }; MyAJAX.prototype.get = function (url) { //用get方法发送请求 var xmlHttp = this.build(); var instanceType = this; this.sendRequest("GET",xmlHttp,url); xmlHttp.send(); xmlHttp.onreadystatechange = function () { instanceType.xmlHttpInstance = xmlHttp; if (xmlHttp.readyState===4 && xmlHttp.status===200){ instanceType.accessedCallback(xmlHttp); }else{ instanceType.failedCallback(xmlHttp); } } }; MyAJAX.prototype.accessedCallback = function (xmlHttp) { this.dispatchMsg(xmlHttp);//成功回应后的分发接口 }; MyAJAX.prototype.failedCallback = function (xmlHttp) { //alert("xml接收失败!");//可重写 }; MyAJAX.prototype.dispatchMsg = function (xmlHttp) { var FunctionType=['Abstruct']; //由子类实现 }; MyAJAX.prototype.isReady = function () { //当有数据发送来之后 return this.msg !== undefined; }; MyAJAX.prototype.getData = function () { //用户获取数据的方法 return this.msg; }; MyAJAX.prototype.getXmlHttp= function () { return this.xmlHttpInstance; }; //-----两个常用的具体实现类-----// /*XML的AJAX请求消息(XMLDoc)*/ function XmlAJAX() { MyAJAX.call(this); } XmlAJAX.prototype = Object.create(MyAJAX.prototype); XmlAJAX.prototype.constructor = XmlAJAX; XmlAJAX.prototype.dispatchMsg = function (xmlHttp) { var FunctionType=['Override']; this.msg = xmlHttp.responseXML; }; /*普通AJAX请求消息(TEXT)*/ function MsgAJAX() { MyAJAX.call(this); } MsgAJAX.prototype = Object.create(MyAJAX.prototype); MsgAJAX.prototype.constructor = MsgAJAX; MsgAJAX.prototype.dispatchMsg = function (xmlHttp) { var FunctionType=['Override']; this.msg = xmlHttp.responseText; }; 回调接口式的封装.js
//-------回调接口的封装--------// //详见 blog.csdn.net/shenpibaipao/article/details/78160994 function AJAXpost(url,sendMsg,callback) { var xmlHttp; if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send(sendMsg); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState===4 && xmlHttp.status===200){ callback(xmlHttp); }else{ //失败的回调接口,可以自己定义。一般情况下用不到。 } } } function AJAXget(url,callback) { var xmlHttp; if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("GET",url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send(); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState===4 && xmlHttp.status===200){ callback(xmlHttp); }else{ //失败的回调接口,可以自己定义。一般情况下用不到。 } } }
本文共计574个文字,预计阅读时间需要3分钟。
javascript(function(OOP封装) { // 常用类型定义 var ClassType=['Abstruct'];
// 构造函数 function MyAJAX() { this.msg=undefined; this.xmlHttpInstance=undefined; }
// 构造函数的原型方法 MyAJAX.prototype.build=function() { // 创建一个xmlHttpRequest对象 var xmlHttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlHttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlHttp=new ActiveXObject(Microsoft.XMLHTTP); } return xmlHttp; };
// 导出MyAJAX类 window.MyAJAX=MyAJAX;})(OOP封装);
(OOP封装)AJAXScript.js/*通用发送模型*/ function MyAJAX(){ var ClassType=['Abstruct']; this.msg = undefined; this.xmlHttpInstance = undefined; } MyAJAX.prototype.build = function () { //创建一个xmlHttpRequest var xmlHttp; if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttp; }; MyAJAX.prototype.sendRequest = function (how2Send,xmlHttp,url) { //根据既定方法发送请求 xmlHttp.open(how2Send,url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); }; MyAJAX.prototype.post = function (url,sendMsg) { //用post方法发送请求 var xmlHttp = this.build(); var instanceType = this; this.sendRequest("POST",xmlHttp,url); xmlHttp.send(sendMsg); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState===4 && xmlHttp.status===200){ instanceType.accessedCallback(xmlHttp); }else{ instanceType.failedCallback(xmlHttp); } } }; MyAJAX.prototype.get = function (url) { //用get方法发送请求 var xmlHttp = this.build(); var instanceType = this; this.sendRequest("GET",xmlHttp,url); xmlHttp.send(); xmlHttp.onreadystatechange = function () { instanceType.xmlHttpInstance = xmlHttp; if (xmlHttp.readyState===4 && xmlHttp.status===200){ instanceType.accessedCallback(xmlHttp); }else{ instanceType.failedCallback(xmlHttp); } } }; MyAJAX.prototype.accessedCallback = function (xmlHttp) { this.dispatchMsg(xmlHttp);//成功回应后的分发接口 }; MyAJAX.prototype.failedCallback = function (xmlHttp) { //alert("xml接收失败!");//可重写 }; MyAJAX.prototype.dispatchMsg = function (xmlHttp) { var FunctionType=['Abstruct']; //由子类实现 }; MyAJAX.prototype.isReady = function () { //当有数据发送来之后 return this.msg !== undefined; }; MyAJAX.prototype.getData = function () { //用户获取数据的方法 return this.msg; }; MyAJAX.prototype.getXmlHttp= function () { return this.xmlHttpInstance; }; //-----两个常用的具体实现类-----// /*XML的AJAX请求消息(XMLDoc)*/ function XmlAJAX() { MyAJAX.call(this); } XmlAJAX.prototype = Object.create(MyAJAX.prototype); XmlAJAX.prototype.constructor = XmlAJAX; XmlAJAX.prototype.dispatchMsg = function (xmlHttp) { var FunctionType=['Override']; this.msg = xmlHttp.responseXML; }; /*普通AJAX请求消息(TEXT)*/ function MsgAJAX() { MyAJAX.call(this); } MsgAJAX.prototype = Object.create(MyAJAX.prototype); MsgAJAX.prototype.constructor = MsgAJAX; MsgAJAX.prototype.dispatchMsg = function (xmlHttp) { var FunctionType=['Override']; this.msg = xmlHttp.responseText; }; 回调接口式的封装.js
//-------回调接口的封装--------// //详见 blog.csdn.net/shenpibaipao/article/details/78160994 function AJAXpost(url,sendMsg,callback) { var xmlHttp; if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send(sendMsg); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState===4 && xmlHttp.status===200){ callback(xmlHttp); }else{ //失败的回调接口,可以自己定义。一般情况下用不到。 } } } function AJAXget(url,callback) { var xmlHttp; if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("GET",url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send(); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState===4 && xmlHttp.status===200){ callback(xmlHttp); }else{ //失败的回调接口,可以自己定义。一般情况下用不到。 } } }

![如何将封装好的[JavaScript][AJAX]类改写为高效的长尾词?](/imgrand/wB25GtLy.webp)