我对ajax相当陌生,但我试图实现两个简单的调用来动态更改使用javascript的页面上的两个单独的div。我一次只使用一个调用没有问题,但是当我使用两个时,看起来第二个xmlhttprequest接管第一个,并写入到两个div中。在单个页面中的两个xmlHttpRequests
我已阅读并使用这两个其他职位既不列出的修复程序试图似乎在我的情况下工作:
Sending two Ajax requests to two different PHP scripts from single javascript function
Using two xmlhttprequest calls on a page
这里是我的相关代码:
function request_handler(url, params, changed_div) {
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
}catch(e) {
req = false;
}
}else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
req = false;
}
}
}
if(req) {
req.onreadystatechange = function(){
if (req.readyState == 4 && req.status == 200){
document.getElementById(changed_div).innerHTML = req.responseText);
}
}
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(params)
return true;
}
return false;
}
以下是使用上述功能的每个请求的基本格式:
request_handler("sample.php", parameters , "sample_div");
道歉,如果我在这里传递简单的东西,我似乎无法让它工作。
changed_div仍然是局部的作用,那么为什么它覆盖掉了? – Vishwanath 2011-01-20 17:20:40