一、问题

ajax请求网关的时候,能够请求到对应的路由服务A,并且成功返回,但是在页面的开发者工具中显示的请求是无响应内容

二、背景

本地开了几个项目,分别有网关,eureka集群,服务A,服务B等,在浏览器打开nginx指向页面的时候,出现了跨域问题

三、解决方式

1.方式1:

添加config配置

config:
  gateway:
    globalcors:
      add-to-simple-url-handler-mapping: true
      corsConfigurations:
        '[/**]':
          allowedOrigins:
            - "http://localhost:8080"
          allowedHeaders:
            - "*"
          allowCredentials: true
          maxAge: 360000
          allowedMethods:
            - GET
            - POST
            - DELETE
            - PUT
            - OPTIONS
            - HEAD

2.方式2:

有HttpServletResponse的请求中,添加

public void request(HttpServletResponse response){

httpResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("Origin"));
httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");

}

3.方式3:(本项目使用方式3解决)

直接在接口类中添加@CrossOrigin注解

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐