日期:2014-05-16  浏览次数:20791 次

兼容IE,FireFox,Chrome和Safari的Ajax
    自己封装的ajax方法,兼容IE,FireFox,Chrome,Fafari等浏览器。方法内包括http请求发送和返回responose xml接收。
 
/*
    author: linyang
    date:  2012-01-30
*/
var myAjax = (function(){
    /*
        paramObj:{
            url: request url,
            method: GET or POST,
            encode: character
        }
        return xmlDOMParser
    */
    var _send_synchronous = function(paramObj){
            var url, method, encode,httpRequest,parser;
            url = paramObj.url;
            method = paramObj.method;
            encode = paramObj.encode;
            if(typeof method == 'undefined')
                    method = 'GET';
            if (window.ActiveXObject) {//IE
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } else {//FireFox,Chrome,Safari
                    httpRequest = new XMLHttpRequest();
                    }
            httpRequest.open(method, url, false);
            if(typeof encode != 'undefined')
                httpRequest.setRequestHeader("Content-Type", "text/html;charset="+encode);
            httpRequest.send();
            if (window.ActiveXObject) {//IE
                parser = new ActiveXObject("Microsoft.XMLDOM");
                parser.async = false;
                parser.load(httpRequest.responseXML);
            } else if (document.implementation.createDocument) {
                try {//FireFox
                    parser = document.implementation.createDocument("", "", null);
                    parser.async = false;
                    parser = (new DOMParser()).parseFromString(httpRequest.responseText, "text/xml");
                } catch(e) {//Chrome,Safari
                    var xmlhttp = new window.XMLHttpRequest();
                    xmlhttp.open("GET", httpRequest.responseText, false);
                    xmlhttp.send(null);
                    parser = xmlhttp.responseXML.documentElement;
                }
            }
            return parser;
        }
    /*
        paramObj:{
            url: request url,
            method: GET or POST,
            encode: character,
            param: post param,
            isAsync: boolean,
            parserHandler: function
        }
    */
    var _send_ajax = function(paramObj){
        var url,method,encode,httpRequest,param,isAsync,parser;
        var parserHandler;
            url = paramObj.url;
            method = paramObj.method.toUpperCase();
            encode = paramObj.encode;
            if(method=='GET')
                    param = null;
                else if(method=='POST')
                    param = paramObj.param;
            isAsync = paramObj.isAsync;
            parserHandler = paramObj.parserHandler;
        if (window.ActiveXObject) {//IE
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } else {//FireFox,Chrome,Safari
                    httpRequest = new XMLHttpRequest();
                }
        httpRequest.onreadystatechange = function(){
                if(httpRequest.readyState==4){
                    if(httpRequest.status==200){
                        if (window.ActiveXObject) {//IE
                                parser = new ActiveXObject("Microsoft.XMLDOM");
                                parser.async = false;
                                parser.load(httpRequest.responseXML);
                            } else if (document.implementation.createDocument) {
                                try {//FireFox
                                    parser = document.implementation.createDocument("", "", null);
                                    parser.async = false;
                                    parser = (new DOMParser()).parseFromString(httpRequest.responseText, "text/xml");
                                } catch(e) {//Chrome,Safari
                                    var xmlhttp = new window.XMLHttpRequest();
                                    xmlhttp.