发起 post 请求时导致2个请求都跨域
1. 问题描述
2. 前端
采取 vue3 框架
async requestPost() {
const { data: res } = await this.$http.post('/post', { id: 1, name: "张三", age: 20 });
if (res.code === "200") {
console.log(res);
}
}
3.后端
采取 springboot 框架
@RequestMapping(value = "/post",method = RequestMethod.POST)
public JsonResult<User> post(HttpServletResponse response,@RequestBody User user) {
response.setHeader("Access-Control-Allow-Origin","http://localhost:3000");
return new JsonResult<>(user,"post success","200");
}
4. 解决方案
在后端代码上,添加上这一行注释,即可解决问题
具体资料:可百度
@CrossOrigin("*")