Ajax同一页面同时执行多个XMLHTTP (2)

本文介绍了一种使用AJAX技术在同一页面上进行多次数据加载的方法。通过具体实例展示了如何确保第二次请求不会覆盖第一次未完成的请求,实现页面元素的有序更新。文中详细解释了JavaScript代码的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=red]2.第一次执行完成后执行第二次请求,可避免第一个请求还没执行完就被覆盖[/color]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX同一页面加载多个测试</title>
<script language="JavaScript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.XMLHttpRequest){ //FireFox
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE6
}catch(e){
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //IE7
}catch (e){
alert('对不起,本页面只支持IE6,IE7,FireFox');
}
}
}
}
function startRequest(tn,what) {
createXMLHttpRequest();
xmlHttp.onreadystatechange = function(){handleStateChange(what);};
xmlHttp.open("GET", "test2.jsp?tn="+tn, true);
xmlHttp.send(null);
}
function handleStateChange(what) {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var result = xmlHttp.responseText;
document.getElementById(what).innerHTML = result;
[color=red] if(what=='msg1'){
alert('第二次');
startRequest('sort_jiaoyi','msg2');
}[/color]
}
}
else {
document.getElementById(what).innerHTML = "正在载入...";
}
}
</script>
</head>
<body onLoad="startRequest('sort_fangwu','msg1');">
<table>
<tr><td>
<div id="msg1" style="width:300px; background:#0099FF"></div>
</td></tr>
<tr><td>
<div id="msg2" style="width:300px; background:#66FF33"></div>
</td></tr>
</table>
</body>
</html>

有更好方法没哈??
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值