js实现 jquery的$.ajax

来源:互联网 发布:淘宝永久封店诈骗 编辑:程序博客网 时间:2024/05/17 08:36
function ajaxFunction(){    var xmlHttp;    try { // Firefox, Opera 8.0+, Safari        xmlHttp = new XMLHttpRequest();    }     catch (e) {        try {// Internet Explorer            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");        }         catch (e) {            try {                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");            }             catch (e) {            }        }    }    return xmlHttp;}//模拟 jquery ajax实现function ajax(json){    var  xmlHttp = ajaxFunction();    xmlHttp.onreadystatechange = function(){        //获取ajax当前的请求状态 ==4处理        if (xmlHttp.readyState == 4) {            if (xmlHttp.status == 200) {//callback就是回调函数                json.callback(xmlHttp.responseText);            }        }    }    xmlHttp.open(json.method, json.url, true);    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    xmlHttp.send(json.data);}window.onload = function(){ajax({url:'<%=basePath%>index.jsp',data:null,method:'post',callback:function(data){alert(data);}});}



0 0