`

ajaxUtil_js略记

阅读更多
function createAjaxObj(){
			var req;
			if(window.XMLHttpRequest){
				req = new XMLHttpRequest();
			}else{
				req = new ActiveXObject("Msxml2.XMLHTTP");  //ie
			}
			return req;
}
	
function sendAjaxReq(method,url,param,asychn,handle200,loading,handle404,handle500){
	//创建XMLHttpRequest对象
	var req = createAjaxObj();
	req.onreadystatechange = function(){
		if(4==req.readyState){  //表示服务器的响应数据已经成功接收
			if(200==req.status){
				if(handle200){
					handle200(req.responseText);
				}
			}else if(404==req.status){
				if(handle404){
					handle404();
				}
			}else if(500==req.status){
				if(handle500){
					handle500();
				}
			}
		}else{
			if(loading){
				loading();
			}
				
		}
	}
	
	if("get"==method.toLowerCase()){
		var s = (param==null)?"":"?"+param;
		req.open(method,url+s,asychn);
		req.send(null);
	}else if("post"==method.toLowerCase()){
		req.open(method, url,asychn);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(param);
	}
	
}

function sendAjaxReqGetXML(method,url,param,asychn,handle200,loading,handle404,handle500){
	//创建XMLHttpRequest对象
	var req = createAjaxObj();
	req.onreadystatechange = function(){
		if(4==req.readyState){  //表示服务器的响应数据已经成功接收
			if(200==req.status){
				if(handle200){
					var xmlDoc = req.responseXML;	
					if(xmlDoc==null){
						var result = req.responseText;
						xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
						xmlDoc.loadXML(result);
					}
					handle200(xmlDoc);
				}
			}else if(404==req.status){
				if(handle404){
					handle404();
				}
			}else if(500==req.status){
				if(handle500){
					handle500();
				}
			}
		}else{
			if(loading){
				loading();
			}
				
		}
	}
	
	if("get"==method.toLowerCase()){
		req.open(method,url+(param==null?"":"?"+param),asychn);
		req.send(null);
	}else if("post"==method.toLowerCase()){
		req.open(method, url,asychn);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(param);
	}
	
}


解决浏览器差异的JS  selectNodes.js
/*
   * 引入本js后,通过prototype解决了原来存在的浏览器差异的问题。
   */
  
    // check for XPath implementation 
    if (document.implementation.hasFeature("XPath", "3.0")) {
        // prototying the XMLDocument 
        XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
            if (!xNode) {
                xNode = this;
            }
            var oNSResolver = this.createNSResolver(this.documentElement)
            var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
            var aResult = [];
            for (var i = 0; i < aItems.snapshotLength; i++) {
                aResult[i] = aItems.snapshotItem(i);
            }
            return aResult;
        }
        
        // prototying the Element 
        Element.prototype.selectNodes = function(cXPathString){
            if (this.ownerDocument.selectNodes) {
                return this.ownerDocument.selectNodes(cXPathString, this);
            }
            else {
                throw "For XML Elements Only";
            }
        }
    }

	
// check for XPath implementation 
if( document.implementation.hasFeature("XPath", "3.0") ) 
{ 
   // prototying the XMLDocument 
   XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) 
   { 
      if( !xNode ) { xNode = this; }  
      var xItems = this.selectNodes(cXPathString, xNode); 
      if( xItems.length > 0 ) 
      { 
         return xItems[0]; 
      } 
      else 
      { 
         return null; 
      } 
   } 
    
   // prototying the Element 
   Element.prototype.selectSingleNode = function(cXPathString) 
   {     
      if(this.ownerDocument.selectSingleNode) 
      { 
         return this.ownerDocument.selectSingleNode(cXPathString, this); 
      } 
      else{throw "For XML Elements Only";} 
   } 
} 


为了导入方便整理包含到一个js文件中: included.js
document.write('<script src="ajaxUtil.js"></script>');
document.write('<script src="selectNodes.js"></script>');
分享到:
评论

相关推荐

    ajaxUtil.js

    js里面直接调用工具类的方法即可, 很详细

    js异步下拉提示

    js文本框异步提示(可以自适应,支持多种灵活调用方式),只一个js文件即可,调用非常方便, &lt;input type="text"onkeyup="ajaxUtil.u_list();" /&gt;

    java json ajax util

    将对集合,对象转换成JSON格式的数据抽出来一个类,备份下。以后自己也可以用

    基于Ajax简单google查询典型实例

    3个常用js封装类AjaxUtil.js,EventUtil.js,build.js 有详细的说明文字 基于google查询数据库的最简单实际例子 近期有时间会写一个基于ajax和Lucene的搜索引擎上传

    ajax-utils:用于 jQuery ajax 的包装器

    ajax-utils 我们经常使用ajax函数。 但这有点痛苦。 $.ajax({ type: 'POST', // some parameters success: function () { // when success }, error: function () { ... // You don't have to write "type", ...

Global site tag (gtag.js) - Google Analytics