我正在構建一個快速網站;由於其性質,我可以承擔它在我的Dropbox公用文件夾中。我試圖讀取使用XMLHttpRequest的一些文本文件:公用Dropbox中的XMLHttpRequest訪問文件
function getParameters() {
var oRequest = new XMLHttpRequest();
var sURL = "http://dl.dropbox.com/u/7828009/Relay%20Fundraising%20Site/parameters.txt";
// var sURL = "file:///parameters.txt";
oRequest.open("GET",sURL,false);
oRequest.onreadystatechange = function (oEvent) {
if (oRequest.readyState === 4) {
if (oRequest.status === 200) {
console.log(oRequest.responseText)
} else {
console.log("Error", oRequest.statusText);
}
}
};
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
try {
oRequest.send(null)
} catch (err) {
alert(err);
}
if (oRequest.status==200) alert(oRequest.responseText);
else alert("Error executing XMLHttpRequest call!");
}
我在發送()線得到一個XMLHttpRequest 101錯誤,我不知道如何解決這個問題。確切的錯誤是
XMLHttpRequest cannot load http://dl.dropbox.com/u/7828009/Relay%20Fundraising%20Site/parameters.txt. Origin null is not allowed by Access-Control-Allow-Origin.
我可以做任何事情來解決這個問題,還是因爲Dropbox本身?
http://dl.dropbox.com/u/7828009/Relay%20Fundraising%20Site/main.htm
新的錯誤是
XMLHttpRequest cannot load http://www.javascripter.net/faq/requested_file.htm. Origin http://dl.dropbox.com is not allowed by Access-Control-Allow-Origin.
我能做些什麼呢?
更新2: D'哦......那一刻我已經離開在測試網址,看看它是否是一個Dropbox的問題。它鏈接到一個外部文件。解決這個問題解決了我的問題。
主要問題是將文件從本地副本運行。從Dropbox公開URL運行修復了問題。謝謝Quentin,間接引導我回答。
他說什麼,也:http://stackoverflow.com/questions/ 3076414 /繞過同一原產地策略 –
我試着從Dropbox公共鏈接運行它,所以它應該是相同的原點,但我仍然收到類似的錯誤。查看更新後的帖子... 傑克,感謝您的鏈接,但我不知道我對它瞭解多少。也許一些示例代碼會有所幫助。 – FeifanZ
@ Inspire48 - 它不應該抱怨無法加載您不會加載的URL。 – Quentin