二xmlHtt prequests在单页xmlHtt、prequests

2023-09-10 17:00:20 作者:·与俄无関

我是相当新的AJAX,但想实现两个简单的调用是动态变化的两个单独的div的使用JavaScript的网页上。我用一个电话的时间没有问题,但是当我用两个好像第二xmlhtt prequest接管第一和写入双方的div。

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.

我已阅读并使用这两个其他职位既不列出该修复程序试图似乎在我的情况下工作:

I've read and tried using the fixes listed on these two other posts both neither seem to work in my case:

Sending从单一的javascript函数 2 Ajax请求到两个不同的PHP脚本

Sending two Ajax requests to two different PHP scripts from single javascript function

使用两个xmlhtt prequest页面上的调用

这里是我的相关code:

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.

推荐答案

这个问题

使用两个xmlhtt prequest页面上的调用

确实的回答你的问题。

在你的 request_handler 功能,您使用的是全局变量 REQ 的被覆盖每次调用时间功能。

In your request_handler function, you're using a global variable req that gets overwritten every time you call that function.

如果你改变它启动:

function request_handler(url, params, changed_div) {
    var req;
    // Rest of your function
}

你会发现,它的工作原理。在这种情况下 REQ 有一个局部范围,当你调用因此不覆盖 request_handler 第二次。

you should find that it works. In this case req has a local scope and so is not overwritten when you call request_handler for the second time.

我能否也建议你认真考虑使用jQuery,原型或道场的喜欢,如果你打算编写Ajax脚本?编写工作的跨浏览器的脚本是很难做的很好,这些框架做了很多跑腿的你。

Can I also suggest that you strongly consider using the likes of jQuery, Prototype or Dojo, if you're planning on writing Ajax scripts? Writing scripts that work cross-browsers is hard to do well and these frameworks do a lot of the legwork for you.