日期:2014-05-16 浏览次数:20334 次
? var httpAdapter;
??? //创建一个httpAdapter对象
??? function GethttpAdapterRequest()
??? {
??????? //httpAdapter = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new httpAdapterRequest();
???????? return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
??? }
???
??? //以Get方式发送请求,默认Content-Type为"text/xml; charset=gb2312"
??? this.GetData = function(url)
??? {
??????? httpAdapter = GethttpAdapterRequest();
?? httpAdapter.Open("GET",url, false);
?? httpAdapter.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
//?? httpAdapter.SetRequestHeader ("SOAPAction","http://tempuri.org/getStr");
?? httpAdapter.Send(context);
?? return httpAdapter;
??? }
???
??? //以POST方式发送请求,默认Content-Type为"application/x-www-form-urlencoded"
??? this.PostData = function (url,context)
??? {
??????? httpAdapter = GethttpAdapterRequest();
??????? httpAdapter.Open("POST",url, false);
?? httpAdapter.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//?? httpAdapter.SetRequestHeader ("SOAPAction","http://tempuri.org/getStr");
?? httpAdapter.Send(context);
?? return httpAdapter;
??? }
???
???
??? this.GetDataInAsync = function (url,callbackMethod)
??? {
??????? httpAdapter = GethttpAdapterRequest();
?? httpAdapter.Open("GET",url, true);
?? httpAdapter.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
//?? httpAdapter.SetRequestHeader ("SOAPAction","http://tempuri.org/getStr");
?? httpAdapter.Send(context);
?? httpAdapter.onreadystatechange = callbackMethod;
??? }
???
??? this.PostDataInAsync = function (url,context,callbackMethod)
??? {
??????? httpAdapter = GethttpAdapterRequest();
??????? httpAdapter.Open("POST",url, true);
?? httpAdapter.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//?? httpAdapter.SetRequestHeader ("SOAPAction","http://tempuri.org/getStr");
?? httpAdapter.Send(context);
?? httpAdapter.onreadystatechange = callbackMethod;
??? }
???
??? //获取指定地址的域名
??? this.GetHost = function(url)
??? {
??????? var host = "null";
??????? var ArryHost=new Array();
??????? if(typeof url == "undefined"|| null == url)
??????? {
??????????? url = window.location.href;
??????? }
??????? var regex = /.*\:\/\/([^\/]*).*/;
??????? var match = url.match(regex);
??????? if(typeof match != "undefined" && null != match)
??????? {
??????????? ArryHost = match[1].split(/:/);
??????????? host =ArryHost[0];
??????? }
??????? return host;
??? }
???
??? //获取当前地址的域名
??? this.GetLocalHost=function()
??? {
?????? return GetHost(document.URL);
??? }
?
? 以前每次写AJAX请求总要写好几个JS函数,包括创建XMLHttpRequest对象、发送请求、成功后的处理函数和失败后的处理函数,下面的JS是把这些东西重构之后参数化的形式,然后放到一个单独的net.js里,后续使用只需引入这个JS,然后新建相应的对象和处理函数即可,可以直接拿来使用
?? var net = new Object();
?? net.READY_STATE_UNINITIALIZED = 0;
?? net.READY_STATE_LOADING = 1;
?? net.READY_STATE_LOADED = 2;
?? net.READY_STATE_INTERACTIVE = 3;
?? net.READY_STATE_COMPLETE = 4;
?
???
?? net.ContentLoader = function(url,onload,onerror,method,params,contentType)
?? {
??? this.req = null;
??? this.onload = onload;
??? this.onerror = (onerror) ? onerror : this.defaultError;
??? this.loadXMLDoc(url,method,params,contentType);
?? }
?
?? net.ContentLoader.prototype = {
???
?? onReadyState: function(){
??????
???? var req = this.req;
???? var ready = req.readyState;
???? if(ready == net.READY_STATE_COMPLETE)
???? {
??????? var httpStatus = req.status;
????