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

查看:35
本文介绍了对本地文件系统的 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.

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

以下是我的 jQuery 代码,它适用于 Firefox 但不适用于 Google chrome:

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 拒绝对 file:// url 的所有 XMLHttpRequest.

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.

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

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天全站免登陆