2017-04-16 85 views
-2

這是我的代碼跨越跨域策略獲取JavaScript

function load() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     document.getElementById("change").innerHTML = 
     this.responseText; 
    } 
    }; 
    xhttp.open("GET", "https://www.example.com/xyz.txt", true); 
    xhttp.send(); 
} 

xhttp.open的網址就是一個例子URL與一個txt文件中的另一個領域。我可以在我的代碼中添加什麼以使其工作?謝謝。

+0

你不能從客戶端。該來源必須允許您訪問,使用JSONP或使用代理。 –

回答

0

要做到這一點的最佳方法是CORS

+0

您能否在此發佈代碼示例?我找不到任何簡單的例子。 –

+0

@WatchYoJet - 這不是代碼問題。你必須配置_服務器_。 [這裏](https://github.com/expressjs/cors)是一個庫,可以幫助你使用Node服務器。 [這裏](https://enable-cors.org/server_apache.html)是Apache的說明。 – Malvolio