作者:进出口产地 | 来源:互联网 | 2023-05-25 08:26
Imfairlynewtoajaxbutamtryingtoimplementtwosimplecallstodynamicallychangestwosepara
I'm fairly new to ajax but am trying to implement two simple calls to dynamically changes two separate divs on a page using Javascript. I have no problems using one call at a time, but when I use two it seems like the second xmlhttprequest takes over the first and writes into both divs.
我是ajax的新手,但我正在尝试实现两个简单的调用,使用Javascript动态更改页面上的两个单独的div。我一次使用一个调用没有问题,但是当我使用两个调用时,似乎第二个xmlhttprequest接管了第一个调用并写入两个div。
I've read and tried using the fixes listed on these two other posts both neither seem to work in my case:
我已阅读并尝试使用这两个其他帖子中列出的修复程序,这两个似乎都不适用于我的情况:
Sending two Ajax requests to two different PHP scripts from single Javascript function
从单个Javascript函数向两个不同的PHP脚本发送两个Ajax请求
Using two xmlhttprequest calls on a page
在页面上使用两个xmlhttprequest调用
And here is my relevant code:
这是我的相关代码:
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;
}
Here is the basic format of each request using the function above:
以下是使用上述函数的每个请求的基本格式:
request_handler("sample.php", parameters , "sample_div");
Apologies if I'm passing something simple up here, I just can't seem to get it to work.
抱歉,如果我在这里简单地传递一些东西,我似乎无法让它发挥作用。
3 个解决方案