ajax接口测试代码,不依赖任何环境

function ajax(url, method, data, f){
    var xhr;
    try{
        xhr=new XMLHttpRequest();
    }
    catch (e){
        try{
            xhr=new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e){
            try{
                xhr=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e){
                alert("Don't support!");
                return false;
            }
        }
    }
    xhr.onreadystatechange = f;
    xhr.open(method, url, false);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send(data);
};

ajax('/login.php', 'POST', 'username=username&password=password', function f(){
    if(this.readyState==4){
        if(this.status=200){
            eval('var obj=' + this.responseText + ';');
            alert(obj.success);
        }
        else{
            alert('Network interactive failure!');
        }
    }
});

 

posted on 2012-04-30 18:13  Jacky Yu  阅读(261)  评论(0编辑  收藏  举报