XMLHttpRequest问题:跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https [英] XMLHttpRequest issue: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

查看:873
本文介绍了XMLHttpRequest问题:跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从存储在同一目录中的本地JSON文件中获取一些数据,我得到以下信息:

Trying to get some data from a local JSON file stored in the same directory, I get this:


以$的价格访问XMLHttpRequest来自起源
的b $ b'file:/// C:/Visual%20Studio%20Code/Practice/customer.json'已被CORS政策阻止:仅支持跨域起源请求
适用于协议方案:http,数据,chrome,chrome扩展名,
https。

Access to XMLHttpRequest at 'file:///C:/Visual%20Studio%20Code/Practice/customer.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

app.js:14 GET file:/// C:/ Visual %20Studio%20Code / Practice / customer.json
net :: ERR_FAILED

app.js:14 GET file:///C:/Visual%20Studio%20Code/Practice/customer.json net::ERR_FAILED

有人可以提出解决方案吗?

Can anyone please suggest a solution?

推荐答案

某些浏览器禁止从本地文件(通过文件加载的资源:方案)。您似乎正在使用其中之一。

Some browsers prohibit any kind of ajax from local files (resources loaded via the file: scheme). You appear to be using one of them.

解决方案是安装本地Web服务器并通过 http:代替。有些IDE直接或通过扩展包含了最少的Web服务器。但是,如果不是这样,则可以很容易地本地安装Apache或nginx,或者您可以使用Express.js或Koa.js或类似的版本来自己安装。

The solution is to install a local webserver and load the resources via http: instead. Some IDEs have a minimal webserver included, either directly or via an extension. But if not, a local install of Apache or nginx is fairly easy to install, or you can roll your own with Express.js or Koa.js or similar.

不是浏览器如何处理 file:资源与 http: / https:资源,因此在进行Web开发时,总是最好使用本地Web服务器,而不是 file:

This isn't the only subtle difference between how browsers handle file: resources vs. http:/https: resources, so when doing web development, it's always best to use a local web server, not file:.

这篇关于XMLHttpRequest问题:跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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