ajax 通用方法

来源:互联网 发布:天龙八部游戏网络歌曲 编辑:程序博客网 时间:2024/05/21 13:55

var xmlHttp;

// 创建XMLHttpRequest对象
function createXMLHttpRequest() {
    try {
        // FireFox, Opera 8.0 +, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            // IE 6.0 +
            xmlHttp = new ActiveXObject('MSXML2.XMLHTTP');
        }
        catch (e) {
            try {
                // IE 5.5 +
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) {
                alert('您的浏览器不支持AJAX!');
                return false;
            }
        }
    }
}

function SendContent(url, type, data, handleMethod) {
    createXMLHttpRequest();

    xmlHttp.open(type, url);
    xmlHttp.onreadystatechange = function() { postBack(handleMethod); };
    xmlHttp.send(data);
}

function postBack(method) {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            //var result = xmlHttp.responseText;  // 返回文本或XML
            // 相关数据处理
            method();
        }
        else {
            alert('the XMLHttpRequest status is ' + xmlHttp.status);
        }
    }
}

原创粉丝点击