最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX: How to use TWO xmlHttpRequest in parallel in ONE function? - Stack Overflow

programmeradmin2浏览0评论

How should I do this?

function(id,id2){

    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
    } else { // code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

First Request:

   xmlhttp.open("GET", ".php?id="+id, true);

   xmlhttp.send();

Second Request:

   xmlhttp.open("GET", ".php?id2="+id2, true);

   xmlhttp.send();

}

Because in this way doesn't works.

I want to make it in plain javascript, so please do not post answers with jQuery or any library etc.

Thanks

How should I do this?

function(id,id2){

    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
    } else { // code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

First Request:

   xmlhttp.open("GET", "http://example./ajax.php?id="+id, true);

   xmlhttp.send();

Second Request:

   xmlhttp.open("GET", "http://example./ajax2.php?id2="+id2, true);

   xmlhttp.send();

}

Because in this way doesn't works.

I want to make it in plain javascript, so please do not post answers with jQuery or any library etc.

Thanks

Share Improve this question edited Aug 29, 2010 at 6:48 Adam Halasz asked Aug 29, 2010 at 6:33 Adam HalaszAdam Halasz 58.4k67 gold badges153 silver badges216 bronze badges 2
  • Use JQuery Ajax. it is simple to use. You will find solution for this. – Muneer Commented Aug 29, 2010 at 6:43
  • Thanks for your answer, but I want to make it in plain Javascript :) – Adam Halasz Commented Aug 29, 2010 at 6:46
Add a ment  | 

2 Answers 2

Reset to default 8

It should work if you create a new xmlhttp object. Currently you are attempting to reuse the same object, which is already performing a query so it will not work.

if you are looking for classic javascript style you can use as the following. But use jQuery as it's simple and prehensive. The one thing to be noted is that the "xmlhr" should be in method (callAjax) scope.

function callAjax(url) {
        var xmlhr;

        if (window.XMLHttpRequest) {
            xmlhr = new XMLHttpRequest();
        } else {
            xmlhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhr.onreadystatechange = function () {
            if (xmlhr.readyState == 4 && xmlhr.status == 200) {
                alert(xmlhr.responseText);
            }
        }
        xmlhr.open("GET", url, true);
        xmlhr.send();
    }

    function myFunction(id1, id2) {
        callAjax("http://example./ajax2.php?id2=" + id1);
        callAjax("http://example./ajax2.php?id2=" + id2);
    }
发布评论

评论列表(0)

  1. 暂无评论