开发者

Adapt ajax for crossdomain

开发者 https://www.devze.com 2023-03-19 08:41 出处:网络
Is it possible adapt this code for crossdomain and how function makeRequest(url) { var http_request = false;

Is it possible adapt this code for crossdomain and how

function makeRequest(url) {

    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
      开发者_JAVA技巧  alert('Cannot create an XMLHTTP instance');
        return false;
    }

    http_request.onreadystatechange = function() { alertContents(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function alertContents(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            receiveData(http_request.responseText);
        } else {
            alert("Îòâåò ñåðâåðà ïîëó÷åí, íî åñòü îøèáêà");
        }         
    } 
}


The same origin policy prevents JavaScript reading data from different origins under normal circumstances.

You can work around with:

  1. A proxy for the data on the page's origin
  2. JSONP
  3. CORS (limited browser support, but possibly good enough for prime time now)
0

精彩评论

暂无评论...
验证码 换一张
取 消