/*-----------------------------
ajax关键代码
将ajax封装成一个类
-----------------------------*/
function createAjax() {
    var xmlHttp = false;
    if (window.XMLHttpRequest) {
        //mozilla
        xmlHttp = new XMLHttpRequest();//ie6不支持XMLHttpRequest
        if (xmlHttp.overrideMimeType) xmlHttp.overrideMimeType("text/xml");
 
 
    }
    else if (window.ActiveXObject) {                
        var MSXML = ['MSXML2.XMLHTTP.5.0', 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
        var i;
        for(i = 0; i < MSXML.length; i++){
            try{
                xmlHttp = new ActiveXObject(MSXML[i]);
                break;
            }
            catch(e){}
        }    
    }
    if (!xmlHttp) { alert("error! fail to create an ajax instance!"); return false; }
 
    this.rep = xmlHttp;
 
    this.sendData = function(method, url, asy, fun) {
        if (asy) {
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    fun(xmlHttp);
                }
            }
            if (method == "POST") {
                xmlHttp.open("POST", url, true);
                xmlHttp.setRequestHeader("Conent-Type", "application/x-www-form-urlencoded");
                xmlHttp.send(null);
            }
            else {
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
            }
        }
        else {
            if (method == "POST") {
                xmlHttp.open("POST", url, false);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttp.send(null);
            }
            else {
                xmlHttp.open("GET", url, false);
                xmlHttp.send(null);
            }
            return xmlHttp;
        }
    }
}

相关文章:

  • 2021-11-07
  • 2021-11-10
  • 2021-07-03
  • 2021-05-22
  • 2021-08-14
  • 2022-02-01
  • 2022-01-16
  • 2021-11-26
猜你喜欢
  • 2021-08-29
  • 2022-01-10
  • 2021-11-20
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2022-01-28
相关资源
相似解决方案