AJAX 通用函数,轻松解决同步异步问题

<script type="text/javascript">
//AJAX 函数(fun为空则同步否异步)
function sendRequest(met,url,fun)
{
if(window.ActiveXObject)
{
xmlHttp
=new ActiveXObject('Microsoft.XMLHTTP');
}
else
{
xmlHttp
=new XMLHttpRequest();
}
if(fun)
{
xmlHttp.open(met,url,
true);
xmlHttp.onreadystatechange
=fun;
xmlHttp.send(
null);
}
else
{
xmlHttp.open(met,url,
false);
xmlHttp.send(
null);
}
}
//测试函数
function Test()
{
//同步执行
sendRequest('GET','server.php');
ExecTest();
//异步执行
sendRequest('GET','server.php',ExecTest);
}
function ExecTest()
{
if(xmlHttp.readyState==4&&xmlHttp.status==200)
{
var result=xmlHttp.responseText;
alert(result);
}
}
</script>
原文地址:https://www.cnblogs.com/smallfa/p/1866698.html