has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
时间: 2023-08-27 15:17:19 浏览: 148
问题的报错信息是:has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 这个错误通常在进行跨域请求时出现。跨域请求是指在一个域下的页面请求另一个域下的资源,由于同源策略的限制,浏览器会阻止这样的请求。具体错误信息说明了请求中缺少了Access-Control-Allow-Origin请求头,导致请求被阻止。解决这个问题的方法是在服务端的响应中添加Access-Control-Allow-Origin请求头,并设置合适的值来允许跨域请求。如果是简单的跨域请求,可以在相应的请求路径映射的方法中通过设置response的头部信息来添加Access-Control-Allow-Origin请求头,比如:
```java
@GetMapping("/{type}")
public ResponseData queryByType(@PathVariable String type, HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin","*");
return picService.queryByType(type);
}
```
这样设置后,浏览器就会允许来自任意域的请求访问该资源了。同时,还可以根据需求设置其他的头部信息,如Access-Control-Max-Age、Access-Control-Allow-Headers和Access-Control-Allow-Credentials等。希望这个解决方法可以帮助到您。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [解决前后端跨域报错:has been blocked by CORS policy: No ‘Access-Control-Allow-Origin](https://blog.csdn.net/wsaicyj/article/details/127814374)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the ...](https://blog.csdn.net/qq_45800640/article/details/118422632)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文