SSM处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题

在开发中,前端同事调用后端同事写好的接口,在地址中是有效的,但在项目的ajax中,浏览器会报 "No 'Access-Control-Allow-Origin' header is present on the requested resource"的错误。

这是由于浏览器禁止ajax请求本地以外的资源,解决办法如下:

后端同事在Controller层的类上增加@CrossOrign注解,当前文件的所有接口就都可以被调用。

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("test")
public class TestController {

@RequestMapping("/one")
public Object one(HttpServletRequest request){
System.out.println("请求成功");
return "请求成功";
}


....

}
原文地址:https://www.cnblogs.com/Im-Victor/p/11496119.html