跨域xmlhttprequest GET问题

时间:2014-12-29 13:31:00

标签: javascript xmlhttprequest cors

我一直在尝试通过javascript执行跨域xmlHttpRequest但是尽管尝试在网上提供的每个解决方案,我都无法做到。我每次都获得0状态。

代码 -

var isIE8 = window.XDomainRequest ? true : false;    
var url = '<any cross domian URL>';    
var resultText = '';   
var invocation = createCrossDomainRequest();    
makeRequest();

function createCrossDomainRequest(url, handler) {
    var request;
    if (isIE8) {
        request = new window.XDomainRequest();
    } else {
        request = new XMLHttpRequest();
    }
    return request;
}

function makeRequest() {
    if (invocation) {
        if (isIE8) {
            invocation.onload = requestSucceeded;
            invocation.open("GET", url, true);
            invocation.send();
        } else {
            invocation.open('GET', url, true);
            invocation.onreadystatechange = handler;
            invocation.send();
        }
    } else {
        resultText = "No Invocation TookPlace At All";
    }
}

function handler(evtXHR) {
alert(invocation.status);
    if (invocation.readyState == 4) {
        if (invocation.status == 200) {
            requestSucceeded();
        } else {
            resultText = "Invocation Errors Occured";
        }
    }
}

function requestSucceeded() {

    resultText = invocation.responseText;
    outputResult();
}

function outputResult() {
    document.getElementById( 'output' ).innerHTML = resultText;
}

请建议解决方案。

0 个答案:

没有答案