No 'Access-Control-Allow-Origin' header is present on the requested resource
时间: 2023-08-18 08:15:17 浏览: 179
第五章-TD-SCDMA通信系统呼叫处理.pdf
这个错误是由于跨域问题引起的。跨域是指在浏览器中,当前端代码通过ajax请求访问不同域名或端口的后端接口时,浏览器会进行安全限制,检查响应头中是否包含了"Access-Control-Allow-Origin"字段。如果没有包含该字段,浏览器就会报错"No 'Access-Control-Allow-Origin' header is present on the requested resource"。\[2\]
解决这个问题的方法有多种。一种方法是在后端接口或配置文件中添加跨域代码,允许所有来源访问。例如,在后端接口中可以添加以下代码:
```
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");
```
这样就可以解决跨域问题了。\[1\]
另外,有时候出现跨域问题是因为配置了多次跨域。例如,在FileUtil中配置了一次,又在CorsFilter中配置了一次,导致出现了两个"Access-Control-Allow-Origin"字段。解决方法是删除其中一个配置即可。\[3\]
#### 引用[.reference_title]
- *1* *3* [No ‘Access-Control-Allow-Origin‘ header is present on the requested resource关于vue跨域问题](https://blog.csdn.net/qq_42212557/article/details/128197716)[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^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题](https://blog.csdn.net/qq_37647296/article/details/89401499)[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^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文