springboot的跨域访问

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

处理 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"的错误。web

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

后端同事在Controller层的类上增长@CrossOrign注解,当前文件的全部接口就均可以被调用。spring

 
  1. import org.springframework.web.bind.annotation.CrossOrigin;后端

  2. import org.springframework.web.bind.annotation.RequestMapping;浏览器

  3. import org.springframework.web.bind.annotation.RestController;app

  4.  
  5. @CrossOriginspa

  6. @RestControllercode

  7. @RequestMapping("test")接口

  8. public class TestController {

  9.  
  10. @RequestMapping("/one")

  11. public Object one(HttpServletRequest request){

  12. System.out.println("请求成功");

  13. return "请求成功";

  14. }

  15.  
  16.  
  17. ....

  18.  
  19.  
  20. }

相关文章
相关标签/搜索