解决Ajax异步访问跨域问题

发布时间 2023-09-10 17:34:25作者: liyongliang的博客

只需要在配置文件中加入如下配置即可:

spring:
  cloud:
     gateway:
        globalcors: # 全局的跨域处理
          add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
          corsConfigurations:
           '[/**]':
          allowedOrigins: # 允许哪些网站的跨域请求,如果是80端口,不要写端口号
           - "http://localhost:8090"
           - "http://127.0.0.1:8090"
           - "http://localhost"
           - "http://127.0.0.1"
          allowedMethods: # 允许的跨域ajax的请求方式
         - "GET"
         - "POST"
         - "DELETE"
         - "PUT"
         - "OPTIONS"
         allowedHeaders: "*" # 允许在请求中携带的头信息
         allowCredentials: true # 是否允许携带cookie
         maxAge: 360000 # 这次跨域检测的有效期