3

I want my page to execute the same function 3 times, calling for 3 different things each time.

The code is currently

 <script>
 ajaxGet('/goget.php?name='+edit.dep.value, 'dep_result');
 ajaxGet('/goget.php?name='+edit.arr.value, 'arr_result');
 ajaxGet('/goget.php?type='+edit.reg.value, 'reg_result');
 </script>

But it only executes the last call. Why?

Javascript:

var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {

XMLHttpRequestObject = new XMLHttpRequest();

} else if (window.ActiveXObject) {

XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function ajaxGet(ajaxLoadData, changeToID) {

if(XMLHttpRequestObject) {

    var obj = document.getElementById(changeToID);
    XMLHttpRequestObject.open("GET", ajaxLoadData);
    XMLHttpRequestObject.onreadystatechange = function() {

    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status ==     200) {

        obj.innerHTML = XMLHttpRequestObject.responseText;

    }
}

    XMLHttpRequestObject.send(null);

}
}
5
  • 1
    And what does the ajaxGet function look like ? Commented Aug 4, 2013 at 22:50
  • 2
    Probably some asynchronous issue, can we see ajaxGet? Commented Aug 4, 2013 at 22:50
  • How did this get a +1? You haven't posted enough code for anybody to debug. Please post ajaxGet. Commented Aug 4, 2013 at 22:53
  • Did you get any error messages in the console? Maybe the first call already causes an error and that's why the next calls will never get executed. Commented Aug 4, 2013 at 22:53
  • You'll have to place all the variables inside the function Commented Aug 4, 2013 at 23:00

2 Answers 2

3

You'll want to have each call to ajaxGet create its own XMLHttpRequest.

An XMLHttpRequest can only handle 1 request at a time. And, since they'll be making the request asynchronously, they'll be processing in parallel rather than sequentially.

function ajaxGet(ajaxLoadData, changeToID) {
    var obj = document.getElementById(changeToID);
    var xhr = new XMLHttpRequest();

    xhr.open("GET", ajaxLoadData);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            obj.innerHTML = xhr.responseText;
        }
    };
    xhr.send(null);
}

Granted, browsers generally limit requests to only a few at a time (usually 2), queuing any additional. But your code shouldn't count on that.


Side note: Unless you need to support IE6 or older, you shouldn't need to fallback to ActiveXObject.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was using a function from an old website i created years ago. :-)
0

You need to change your ajaxGet() method to instantiate a new XMLHttpRequest object at every call. Since, you're using the same XMLHttpRequest every new request is overwriting the previous one.

Hence, you're getting the results returned by the last method call only.

function getXHRObj() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function ajaxGet(ajaxLoadData, changeToID) {
    var XMLHttpRequestObject = getXHRObj();
    var obj = document.getElementById(changeToID);
    XMLHttpRequestObject.open("GET", ajaxLoadData);
    XMLHttpRequestObject.onreadystatechange = function() {
        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
            obj.innerHTML = XMLHttpRequestObject.responseText;
        }
    }
    XMLHttpRequestObject.send(null);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.