Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee My腾云code

Happy_EveryDay

可以平凡 不可以平庸 无爱则无忧,无欲则无求,无怒而无敌,无怨才是佛。所有烦恼,都是放不下的执著 开源技群 328035181 MyGitee

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>

posted on 2010-11-01 22:55  cn2025  阅读(243)  评论(0)    收藏  举报

导航