javascript - XDomainRequest (CORS) för XML som orsakar "Åtkomst nekad"-fel i IE8/IE9


Ursäkta om detta verkar vara en dubblett, men jag kan inte se ett tydligt svar på någon av de liknande frågorna.

När jag försöker göra en CORS-förfrågan för viss XML får jag ständigt ett JS-fel från IE8 som säger 'Åtkomst nekad'.

Min kod är anpassad från detta exempel:

// Create the XHR object.
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}

// Helper method to parse the title tag from the response.
function getTitle(text) {
  return text.match('<title>(.*)?</title>')[1];
}

// Make the actual CORS request.
function makeCorsRequest() {
  // All HTML5 Rocks properties support CORS.
  var url = 'http://updates.html5rocks.com';

  var xhr = createCORSRequest('GET', url);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }

  // Response handlers.
  xhr.onload = function() {
    var text = xhr.responseText;
    var title = getTitle(text);
    alert('Response from CORS request to ' + url + ': ' + title);
  };

  xhr.onerror = function() {
    alert('Woops, there was an error making the request.');
  };

  xhr.send();
}

från http://www.html5rocks.com/en/tutorials/cors/

Detta bör fungera i IE8 med XDomainRequest, och när jag laddar exempel-sidan och klickar på "Kör exempel" på html5rocks-sidan, fungerar det i IE8. Men så snart jag kopierar koden till min egen sida och kör, får jag felet "Access is denied" på xhr.open()-raden inne i XDomainRequest.

Den här har verkligen förbryllat mig - servern är definitivt korrekt inställd så det är något som har att göra med frontend. Tack på förhand till alla som kan hjälpa!

Svar #1

OK, problemet berodde på konstigheter i IE8 & 9 som löstes med några förslag från den här artikeln: http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/ (främst genom att sätta några tomma handler-funktioner och omsluta .send() i en 0-tidsgräns).

Här är min slutgiltiga kod som fungerar i IE8/9/10/11 och FF/Chrome:

function doRequest(url) {

    // Create the XHR object.
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open('get', url, true);
    }else if(typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open('get', url);
    }else{
        // CORS not supported.
        xhr = null;
    };

    if (!xhr) {
        return;
    };

    // Response handlers.
    xhr.onload = function() {
        //do what you want with the response. Remember to use xhr.responseText for ie8 compatibility, because it doesn't have .responseXML
        if(xhr.responseXML) {
            xml = this.responseXML;
        }else if(xhr.responseText){
            xml = new ActiveXObject('Microsoft.XMLDOM');
            xml.loadXML(xhr.responseText);
        };
    };

    xhr.onerror = function() {
        //do what you want on error
    };

    //these blank handlers need to be set to fix ie9 http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/
    xhr.onprogress = function () { };
    xhr.ontimeout = function () { };

    //do it, wrapped in timeout to fix ie9
    setTimeout(function () {
                xhr.send();
            }, 0);

};