AJAX无法在Internet Explorer 9中运行

This is my function:

<script type="text/javascript">
function loadXMLDoc() {
    var x = document.getElementById("trazi_drzava");
    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");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("trazi_grad").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "gradovi.php?selected=" + x.value, true);
    xmlhttp.send();
}
</script>

and I call it like this:

 <select name="td" id="trazi_drzava" onchange="loadXMLDoc()">
    <option value="">Država</option>
    <?php
    $sel_grad_arr=array();
    $sel_grad_arr[]="<select name='tg' id='grad0'>  
    <option value=''>Grad</option</select>";
    if($q=mysql_query("SELECT drzava_id,drzava FROM drzava")){
    while($r=mysql_fetch_assoc($q)){
        echo '<option value="'.$r['drzava_id'].'">'.$r['drzava'].'</option>';
       }
     }else echo mysql_error().__LINE__;
    ?>
    </select><select name="tg" id="trazi_grad">
      //code that ajax should load        
    </select>

It works fine with most browsers, but with Internet Explorer 9 it doesn't work at all. Anyone have any idea why?

UPDATE: I didn't manage to do this then. So I changed logic of working totaly. Thanks everyone for answers.

I know this is a very old question, but still, one with no actually correct answer....

The correct order of operations is:

  1. Create your request object
  2. open a connection
  3. Set onreadystatechange listener
  4. send request

You have steps 2 and 3 in the wrong order, which causes problems in certain browsers.

Maybe it's a cache problem. Parameter test set as a timestamp value, something like:

xmlhttp.open("GET","gradovi.php?selected="+x.value+"&t="+parseInt(new Date().getTime().toString().substring(0, 10)),true);
xmlhttp.send();

Regards!

Check how your request is sent to server. What does the double quotes look like? IE9, at least with jQuery, doesn't encode double quotes correctly according to this post: Why does this jQuery Ajax call fail ONLY in IE9 (Even works fine in IE8 and IE7)

This is too late to answer but here's the solution that should solve this problem.

I had a similar problem this week. Change the

xmlhttp.open("GET", "gradovi.php?selected=" + x.value, true);

to

xmlhttp.open("POST", "gradovi.php?selected=" + x.value, true);

This solved my problem. Check here: This PHP script doesn't work in Internet explorer and Microsoft Edge but works in Chrome/Firefox/Safari/Opera