none
Ajax Script not working in IE (window.XMLHttpRequest) RRS feed

  • Question

  • Hi,

    I have written following code in ajax.

    This is a javascript function called on onchange event of a button.

    This code works fine in firefox and chrome but does not work in IE8.0+
    the values don't get displayed.

    plz suggest me if there is any change.

    thnx in advance.


    <script type="text/javascript">
        
    function showUser(str) {
      if (str=="") {
        document.getElementById("userDetailsShownHere").innerHTML="";
        return;
      }
      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("userDetailsShownHere").innerHTML=xmlhttp.responseText;
          document.getElementById("btn").name=str;
            showGift(str);
        }
      }
      xmlhttp.open("GET","getuser.php?username="+str,true);
      xmlhttp.send();
    }
    //This code will execute on button click
    function showGift(str) {
        var life = document.getElementById("lifeLabel").value;
        var number = parseInt(life, 0);
        if(number == 55){      
            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("userDetailsShownHere").innerHTML=xmlhttp.responseText;
             document.getElementById("btn").name=str;
           document.getElementById("btn").style.display="inline";
           
            }
          }
          xmlhttp.open("GET","getuser.php?username="+str,true);
          xmlhttp.send();
        }
        else if(number > 0){
          document.getElementById("noLife").style.display="none";
            number = number - 1;

          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("userDetailsShownHere").innerHTML=xmlhttp.responseText;
              document.getElementById("btn").name=str;
            document.getElementById("btn").style.display="none";
             if(document.getElementById("cometomorrow") || document.getElementById("win")){
                document.getElementById("btn").style.display="none";
                console.log("hide");
              }else{
                setTimeout("showButton()", 1000);
              }
            }
          }
          xmlhttp.open("GET","getgift.php?username="+str+"&loop="+number,true);
          xmlhttp.send();
        
        }
        else {
        document.getElementById("userDetailsShownHere").innerHTML="";
            document.getElementById("noLife").style.display="inline";
        document.getElementById("btn").style.display="none";
        }
    }

    </script>





    • Edited by Vishalika Thursday, November 27, 2014 7:39 AM
    Thursday, November 27, 2014 7:36 AM

All replies

  • Hi Vishalika,

    Please post questions about html, css and scripting for website development to

    MSDN IE Web Development forum is moving to stack-overflow

    xmlhttpRequests is not available to webpages using the file: protocol in MSIE browsers.

    If possible include with your questions a link to your website or a mashup that shows the issue.

    If you are running the webpage from your file system (file: protocol), please advise as such.

    f12>Debug tab to debug your webpage scripts and include any error messages from the console with your scripts... (however xmlhttprequests to local resources do not cause errors in the Developer tool console).... you should always test your web applications from an actual web server first (localhost).

    Questions regarding Internet Explorer 8, 9 and 10 and Internet Explorer 11 for the IT Pro Audience. Topics covered are: Installation, Deployment, Configuration, Security, Group Policy, Management questions. If you are a consumer looking for answers or to raise a question, it's highly recommended you head on over to http://answers.microsoft.com/en-us


    Rob^_^

    Thursday, November 27, 2014 11:11 PM
  • This is what I used and I'm still testing, but I like it so far.


                var xmlHttp = null;
                if (window.XMLHttpRequest) {
                    xmlHttp = new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    var versions = [
                        "MSXML2.XmlHttp.6.0",
                        "MSXML2.XmlHttp.5.0",
                        "MSXML2.XmlHttp.4.0",
                        "MSXML2.XmlHttp.3.0",
                        "MSXML2.XmlHttp.2.0",
                        "Microsoft.XmlHttp"];
                    var xhr;
                    for (var i = 0; i < versions.length; i++) {
                        try {
                            xmlHttp = new ActiveXObject(versions[i]);
                            break;
                        } catch (e) {
                        }
                    }
                }
                if (xmlHttp != null) {
                    xmlHttp.onreadystatechange = function () {
                        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                            document.getElementById("divLOAD").style.display = "none";
                            var data = JSON.parse(xmlHttp.responseText);
                            if (data == "" && sIsAutoComplete == "F") {
                                PlateResult("", "", "", "", "RED", sSearch);
                                return;
                            }
                            else if (data == "") {
                                return;
                            }
                            if (sIsAutoComplete == "T") {
                                return AutoComplete(data);
                            }
                            else if (sIsAutoComplete == "F") {
                                SearchPlateShow(data, "F");
                            }
                            else if (sIsAutoComplete == "A") {
                                s_CacheSelectedCompanyID = s_SelectedCompanyID;
                                AllData = data;
                                var dt = new Date();
                                CacheDate.setTime(dt.getTime() + (CacheMinutes * 60 * 1000)); //1 minute //days (exdays * 24 * 60 * 60 * 1000) || minute (exdays * 60 * 1000)
                            }
                        }
                    }
                    xmlHttp.onerror = function () {
                        document.getElementById("divLOAD").style.display = "none";
                        if (sIsAutoComplete == "T") {
                            return AutoComplete(AllData);
                        }
                        else if (sIsAutoComplete == "F") {
                            SearchPlateShow(AllData, "T");
                        }
                    }
                    xmlHttp.open("POST", "/Home/SearchPlate/", true);
                    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlHttp.send('__RequestVerificationToken=' + sToken + '&sLicensePlate=' + Encode(sLicensePlate) + '&sIsAutoComplete=' + sIsAutoComplete + '&sID=' + s_SelectedCompanyID + '&sKey=' + sKey + '&sIV=' + sIV);
                }
                else {
                    //Browser not supported! Please update your browser!
                    document.getElementById("spParkingTracker").innerHTML = "Browser not supported!";
                }

    Monday, October 15, 2018 5:38 PM