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>');