我有一個作爲配置編輯器的網頁,這意味着它將通過打開.html文件並且不使用HTTP。使用File:///,在Firefox中工作的AJAX打開頁面,而不是Chrome或IE。
此頁面需要訪問位於同一目錄中的另一個文件(要編輯的配置文件)。該文件使用相對路徑General.json
進行訪問。
var getJSONFileContent = function(url) {
return $.ajax({
type: "GET",
url: url,
async: false
}).responseText;
};
var currentConfigAsJson = getJSONFileContent("General.json");
它在Firefox上完美工作,無需更改設置或任何設置,但在IE和Chrome上均失敗。
Chrome的錯誤:
file:///C:/Users/XXX/Desktop/XXX/General.json.
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.m.ajaxTransport.send
@ jquery-1.11.3.min.js:5m.extend.ajax
@ jquery-1.11.3.min.js:5getJSONFileContent
@ General.html:68(anonymous function)
@ General.html:75m.Callbacks.j
@ jquery-1.11.3.min.js:2m.Callbacks.k.fireWith
@ jquery-1.11.3.min.js:2m.extend.ready
@ jquery-1.11.3.min.js:2J
@ jquery-1.11.3.min.js:2
Internet Explorer錯誤:
SCRIPT5: Access denied.
Fichier : jsoneditor.min.js, line : 7, column : 8725
我看,這是在Chrome(也可能是IE等)出於安全原因禁止的,而且我不得不開始鉻與特殊的參數繞過這一點。
但爲什麼它在Firefox上開火?有沒有辦法使Chrome在Chrome中運行,而無需在運行Chrome時傳遞特殊參數?
是否有Chrome的特定功能,可以讓我讀取/寫入文件而不必更改設置或傳遞參數?一個終端用戶不會想爲此煩惱。
謝謝。 :)
[在Chrome中禁用相同來源策略]的可能重複(http://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome) – Hacketo
每個瀏覽器都有自己的本地文件保護策略。您可以更改它:https://wiki.fluidproject.org/display/fluid/Browser+settings+to+support+local+Ajax+calls – Krzysiek
Chrome不允許ajax運行'file:' – lshettyl