2

I have a piece of code that works in chrome and firefox but not in internet explorer. I can't figure out what that actual cause is. I get a operation timeout message from internet explorer "Message: The operation was timed out."

This is the ajax function I am using, it's from w3schools so I know this is correct.

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

    alert(xmlhttp);
    return xmlhttp;
}

This is the code that's getting stuck on. The error message is in "ajaxRequest.send(postdata);".

function edit(){
    var ajaxRequest = ajax();   
    var postdata = "data=" + document.getElementById("id1").value + "<|>" + document.getElementById("id2").value + "<|>" +  document.getElementById("id3").value;


    ajaxRequest.onreadystatechange = function(){
            var ajaxDisplay = document.getElementById('ajaxDiv');
        if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }   
    alert(postdata);
    ajaxRequest.open("POST","confirmPage.php",false);
    ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajaxRequest.send(postdata);
    alert("Finished");
}

All the other pages work with the same code in internet explorer but not this particular page. I can't seem to figure to out why. This page works in chrome and firefox but not in internet explorer. It never goes to "Finished". I am using IE 8.

theking963
  • 2,223
  • 7
  • 28
  • 42

2 Answers2

0

As you require synchronous behaviour try the following:

ajaxRequest.open("POST", "confirmPage.php", false);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send(postdata);

if (ajaxRequest.status === 200) {
    document.getElementById('ajaxDiv').innerHTML = ajaxRequest.responseText;
}

alert("Finished");

You don't need the onreadystatechange as the request is synchronous.

jabclab
  • 14,786
  • 5
  • 54
  • 51
  • Yes I know I need a synchronous request. If I change it to `'true'` it's alert's `"Finished"` but doesn't go to the `page(confirmPage.php)`. And obviously it doesn't provide the error because it's not waiting for a response. – theking963 Dec 12 '11 at 15:59
  • Does the status of the Ajax request equal `200`? – jabclab Dec 12 '11 at 16:19
  • I am not sure. I tried to print alert statement for 200, 301, 302, 304, 307, 400, 401, 403, 404, 410, 500 and 501. It's neither of these. Am I missing anything else? – theking963 Dec 12 '11 at 16:35
0

Partial answer:

I figured out the problem. It's not javascript and/or ajax. IE can't handle a large number of results in queries so it times out. It's a very obscure error as I was thinking it's something to do with the ajax functions rather than the php file.

The result set is not huge. There are 5 different queries. Each one with about 5-50K records(I am not printing all of them, just querying). It times out after a large result set.

To test I created a test page with simple SELECT * queries and it can handle only 2-3 queries. If it's more than that it times out.

theking963
  • 2,223
  • 7
  • 28
  • 42