状态码200 No 'Access-Control-Allow-Origin' header is present on the requested resource.
时间: 2023-10-29 21:55:05 浏览: 205
当状态码为200,并且错误信息为"No 'Access-Control-Allow-Origin' header is present on the requested resource"时,表示服务器没有返回正确的跨域请求头。这个错误通常出现在浏览器中,因为浏览器强制执行同源策略,不允许在一个域中访问另一个域的资源。
解决这个问题的方法是在服务器端添加正确的跨域请求头。服务器收到请求后会判断请求的来源是否允许跨域,如果允许,则在响应头中添加"Access-Control-Allow-Origin"字段,并指定允许的来源。例如,可以在响应头中添加"Access-Control-Allow-Origin: http://localhost:8601",表示只允许来自"http://localhost:8601"的请求跨域访问。
通过在服务器端配置正确的跨域请求头,浏览器就可以正确地处理跨域请求,从而避免"No 'Access-Control-Allow-Origin' header is present on the requested resource"的错误。
相关问题
No Access-Control-Allow-Origin header is present on the requested resource.
No 'Access-Control-Allow-Origin' header is present on the requested resource是一个跨域问题。这个问题通常发生在前端访问后端接口时,由于安全限制,浏览器会阻止跨域请求。要解决这个问题,有几种方法可以尝试。
一种方法是在后端接口或配置文件中添加跨域代码。可以使用以下代码来允许所有来源访问接口:
header('Access-Control-Allow-Origin:*');//允许所有来源访问
header('Access-Control-Allow-Method:POST,GET');//允许访问的方式
或者在过滤器中添加以下代码:
httpResponse.setHeader("Access-Control-Allow-Origin","*");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
httpResponse.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
另一种可能的原因是在配置文件或其他设置中重复配置了跨域。比如在FileUtil中配置了response.addHeader("Access-Control-Allow-Origin", "*"),然后又在CorsFilter中添加了一次配置。这样就会出现两个Access-Control-Allow-Origin问题。在这种情况下,只需要删除其中一个配置即可解决问题。
总结来说,解决"No 'Access-Control-Allow-Origin' header is present on the requested resource"问题的方法是添加跨域代码或删除重复的跨域配置。这样浏览器就能正确处理跨域请求了。
No 'Access-Control-Allow-Origin' header is present on the requested resource.
This error occurs when the requested resource does not include the "Access-Control-Allow-Origin" header. This header is required to allow cross-origin resource sharing (CORS) and is used to restrict which origins can access the resource.
To resolve this issue, you can add the "Access-Control-Allow-Origin" header to the server response. The value of this header should be the origin (domain) from which the request originates or "*" to allow access from any origin.
Here is an example of how to include the header in a server response using JavaScript:
```
response.setHeader("Access-Control-Allow-Origin", "*");
```
Please note that allowing access from any origin using "*" can pose security risks, so it's recommended to restrict access to specific origins whenever possible.
If you are not the owner of the server or do not have access to modify the server-side code, you can try reaching out to the server administrator or the website owner to address this issue.
阅读全文