Javascript 对本地文件系统的 AJAX 请求在 Chrome 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38344612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:22:14  来源:igfitidea点击:

AJAX request to local file system not working in Chrome?

javascriptjqueryajaxgoogle-chrome

提问by vibog

I am working to dynamically create a UI from XML using jQuery. My jQuery is working in Firefox but in Chrome it's not working. It gives me this console error:

我正在使用 jQuery 从 XML 动态创建 UI。我的 jQuery 在 Firefox 中工作,但在 Chrome 中它不起作用。它给了我这个控制台错误:

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource。

The following is my jQuery code which working on Firefox but not working on Google chrome:

以下是我在 Firefox 上运行但在 Google chrome 上不运行的 jQuery 代码:

$.ajax({
    url: 'file:///home/satendra/dndExamples/avisDnD/file.xml',
    success: function(xml) {
        $(xml).find('Tab').each(function() {
            var id = $(this).attr('URL');
            var tab = $(this).attr('TabName');
            $("ul").append("<li><a href="+ id +">"+ tab +"</li>");
        });
    }
});

回答by Rory McCrossan

Firefox allows the request because it accepts requests to the local file system (ie. the file://protocol) if they originate from there too. However Chrome denies all XMLHttpRequests to file://urls.

Firefox 允许请求,因为它接受对本地文件系统(即file://协议)的请求,如果它们也来自本地文件系统。但是,Chrome 拒绝对file://url 的所有 XMLHttpRequest 。

Note that you cannot make an AJAX request to the local file system from an external domain in either browser - it would be a massivesecurity flaw if you could.

请注意,您不能在任一浏览器中从外部域向本地文件系统发出 AJAX 请求 - 如果可以,这将是一个巨大的安全漏洞。

For this AJAX request to work in Chrome you need to make the request to a webserver. If you're on Windows you can easily install IISor WAMPon your local machine.

要在 Chrome 中运行此 AJAX 请求,您需要向网络服务器发出请求。如果您使用的是 Windows,则可以轻松地在本地计算机上安装IISWAMP

Note that it is possible to enable a setting in Google Chrome which allows requests to the local file system from the browser, but it's really not a good idea to use it. If you decide you want to go ahead and do this anyway, a guide can be found here.

请注意,可以在 Google Chrome 中启用允许从浏览器向本地文件系统发出请求的设置,但使用它确实不是一个好主意。如果您决定继续执行此操作,可以在此处找到指南。