Access to XMLHttpRequest at 'file:///C:/Users/%E6%9D%8E%E5%8D%8E%E6%98%AD/Desktop/material/003-ajax%E5%9F%BA%E7%A1%80/1.json' 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, https, chrome-untrusted.
时间: 2024-02-05 18:02:45 浏览: 487
这个错误是由于浏览器的安全策略引起的。浏览器通常会阻止跨域请求(CORS),即从一个源(origin)发出的请求不能访问另一个源的资源。
在这种情况下,你正在尝试从本地文件系统(file://)的源发出请求,而目标资源位于一个不同的源(null)。浏览器默认情况下不允许这样的跨域请求。
要解决这个问题,你可以将资源部署到一个支持跨域请求的服务器上,然后通过HTTP或HTTPS协议访问它。或者,你可以尝试使用一些工具来模拟一个本地服务器,例如使用Python的`SimpleHTTPServer`模块或安装一个本地开发服务器。
如果你只是想在本地测试和查看JSON文件,可以尝试使用一些浏览器插件或者命令行工具来启动一个本地HTTP服务器,并从该服务器上访问文件。
相关问题
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 'http://localhost:9091/Classification/selectOrderBySuborder/%E5%8E%9F%E5%85%BD%E4%BA%9A%E7%BA%B2' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是一个跨域请求的错误信息,这个错误通常会出现在前端向后端发送请求时,请求的域名与当前页面的域名不一致时出现。为了解决这个问题,需要在后端服务器中设置响应头信息,添加Access-Control-Allow-Origin字段,指定允许跨域请求的域名。可以参考以下代码:
```
response.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
```
其中,Access-Control-Allow-Origin指定允许跨域请求的域名,可以设置为"*"表示所有域名都允许跨域请求。Access-Control-Allow-Methods指定允许的请求方法,Access-Control-Max-Age指定预检请求的有效期,Access-Control-Allow-Headers指定允许的请求头信息。
阅读全文