0

I've looked through answers but can't seem to find one that fixes this particular problem. Also a similar question is still unanswered.

I am using IE8 to send a XMLHttpRequest but it never gets sent. IE8 supports XMLHttpRequest objects, so why isn't it sending? (I can't use jQuery for this.)

My code:

window.onload = function() {
    // Create the HTTP object
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        alert("couldn't load data");
    }

    var dataUrl = "./data2.json";
    alert("sending");
    xmlhttp.open("GET", dataUrl, true);
    alert("sent");

    xmlhttp.onreadystatechange = function() {
        //ready?
        if (xmlhttp.readyState != 4)
            return false;

        //get status:
        var status = xmlhttp.status;

        //maybe not successful?
        if (status != 200) {
            alert("AJAX: server status " + status);
            return false;
        }

        //Got result. All is good.
        alert(xmlhttp.responseText);

        return true;
    }
    xmlhttp.send(null);
}

The alert('sent'); never gets called, but alert('sending'); does. How can I make this xmlhttp request work?

Edit: I've updated my code to include XDomainRequest for IE8. This gets me past the xmlhttp.open part, but now I'm getting an "Unspecified error" on the xmlhttp.send part.

edited code (Note: I changed my variable name from xmlhttp to xhr.):

window.onload = function() {
    // Create the HTTP object
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        alert("couldn't load data");
    }

    var dataUrl = "./data2.json";
    alert("sending");

    if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Safari
        xhr.open('get', dataUrl, true);
    }else if (typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open('get', dataUrl);
        alert("opened");
    } else{
        alert('CORS not supported');
    };

    xhr.onprogress = function () { };
    xhr.ontimeout = function () { };
    xhr.onerror = function () { alert('error'); };
    xhr.onload = function() {
     alert(xhr.responseText);
    }
    setTimeout(function () {xhr.send();}, 0);
}

alert('error'); gets called.

brienna
  • 1,415
  • 1
  • 18
  • 45
  • Does IE8 (wtf are you doing running IE8!!) have a developer tools console? check it for errors if it does. If it doesn't, then perhaps you see another reason not to use IE8 :p – Jaromanda X Feb 25 '18 at 22:26
  • School assignment :( I wouldn't go near IE otherwise. Just found the developers tool console. It breaks on the xmlhttp.open line, with an error message "Access denied" – brienna Feb 25 '18 at 22:28
  • well, there you go - probably because you're loading the page using `file:///` protocol, rather than `http://` or `https://` - I think you need to enable running javascript on local files - not sure if that's an option somewhere or something you need to do when loading the page - my windows XP computer is stuck in the year 2011 :p – Jaromanda X Feb 25 '18 at 22:30
  • 1
    Try to change the path `var dataUrl = "./data2.json";` so it doesn't use a relative path – Pavlo Feb 25 '18 at 22:50
  • Just did that & updated my code -- still unable to make this request work – brienna Feb 25 '18 at 22:53
  • If it's a local file, it could be a request origin problem. Try https://stackoverflow.com/a/6888068/816185 – comp500 Feb 25 '18 at 23:19
  • Even with a remote link, the problem persists. It's with the xhr.send()? – brienna Feb 25 '18 at 23:38
  • Nothing works. I used the code at https://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ word for word but it STILL throws the "unspecified error" – brienna Feb 26 '18 at 00:11
  • move `onreadystatechange` _before_ `open` – Knu Apr 27 '18 at 00:48

0 Answers0