本地文件系统的AJAX请求在Chrome中不起作用? [英] AJAX request to local file system not working in Chrome?

查看:199
本文介绍了本地文件系统的AJAX请求在Chrome中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


只有协议方案支持交叉源请求:http,data,chrome,chrome-extension,https ,chrome-extension-resource。

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

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

$.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>");
        });
    }
});


推荐答案

Firefox允许请求,因为它接受本地请求文件系统(即 file:// 协议),如果它们源自那里。但Chrome拒绝所有XMLHttpRequests < file:// url。

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.

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

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

为了使这个AJAX请求在Chrome中运行,您需要向Web服务器发出请求。如果您在Windows上,则可以轻松安装 IIS WAMP

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 IIS or WAMP on your local machine.

请注意,您可以在Google Chrome中启用允许从浏览器访问本地文件系统的设置,但使用它并不是一个好主意。如果您决定继续并继续操作,可以参阅这里

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.

这篇关于本地文件系统的AJAX请求在Chrome中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆