Access to XMLHttpRequest at 'file:///C:/Users/A000685/Desktop/0608/cd_catalog.xml' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome-untrusted, https, edge. cd_catalog.xml:1 Failed to load resource: net::ERR_FAILED
时间: 2024-04-07 22:28:59 浏览: 11
这错误是因为浏览器默认禁止加载本地文件。这是出于安全原因,防止JavaScript代码对本地计算机进行恶意操作。如果你想要在浏览器中加载本地文件,可以通过启动一个本地服务器来实现。
可以使用一些工具来启动本地服务器,例如Node.js的http-server模块和Python的SimpleHTTPServer模块。
使用http-server模块的方法如下:
1. 安装Node.js
2. 在终端中运行 `npm install -g http-server` 命令安装http-server模块
3. 进入包含`cd_catalog.xml`文件的目录
4. 在终端中运行 `http-server` 命令启动本地服务器
5. 在浏览器中访问`http://localhost:8080/cd_catalog.xml`即可加载文件
使用Python的SimpleHTTPServer模块的方法如下:
1. 安装Python
2. 在包含`cd_catalog.xml`文件的目录中打开终端
3. 在终端中运行 `python -m SimpleHTTPServer` 命令启动本地服务器
4. 在浏览器中访问`http://localhost:8000/cd_catalog.xml`即可加载文件
注意:在启动本地服务器时,需要将文件名正确地替换为`cd_catalog.xml`。
相关问题
Access to XMLHttpRequest at 'file:///E:/projects/ThreejsSpace/fbxTest/Angry.fbx'
Access to XMLHttpRequest at 'file:///E:/projects/ThreejsSpace/fbxTest/Angry.fbx is a security feature implemented in web browsers to prevent cross-origin requests. It means that you are trying to make an XMLHttpRequest to a local file on your computer, which is not allowed due to security restrictions.
To access the file 'Angry.fbx' using XMLHttpRequest, you need to host it on a web server and access it through a valid URL. This can be done by setting up a local web server or uploading the file to a remote server.
Access to XMLHttpRequest at 'file:///D:/vue/vue-first/data.json' from origin 'null' has been blocked by CORS policy
回答: 这个错误是由于浏览器的CORS策略引起的。CORS是一种安全机制,用于限制跨域请求。当浏览器发现请求的源与目标不在同一个域时,会发送一个预检请求,检查目标服务器是否允许跨域请求。如果目标服务器没有正确配置CORS头部,浏览器就会拒绝该请求,从而导致这个错误。\[1\]\[2\]
对于你提到的具体错误,'file:///D:/vue/vue-first/data.json'是一个本地文件路径,而'null'是请求的源。由于这是一个本地文件请求,而不是通过HTTP协议请求,所以CORS策略不适用于这种情况。因此,你可以通过将数据文件放在与你的应用程序相同的域中,或者使用服务器来提供数据,以避免这个错误。
#### 引用[.reference_title]
- *1* [Access to XMLHttpRequest at ‘http://xxx‘ from origin ‘http://xxx‘ has been blocked by CORS ...](https://blog.csdn.net/qq_41470439/article/details/109361842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [前后端分离跨域问题Access to XMLHttpRequest at ‘http://localhos...has been blocked by CORS policy: ...](https://blog.csdn.net/qq_42416602/article/details/121731774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Access to XMLHttpRequest at ‘http://xx‘ from origin ‘http://xx‘ has been blocked by CORS policy](https://blog.csdn.net/weixin_51603038/article/details/129319187)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]