zoukankan      html  css  js  c++  java
  • nginx解决跨域问题!

    1、问题背景:前端调用线上后段时出现跨域问题!

    解决方法nginx的location头部增加配置:

                     add_header 'Access-Control-Allow-Headers' '*';
                     add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,HEAD,PUT';
                     add_header 'Access-Control-Allow-Origin' '*';
                     add_header 'Access-Control-Allow-Credentials' 'true'
    本地前端调用php接口时需在后端的 location字段都加配置。

    2、当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is present on the requested resource,需要给Nginx服务器配置响应的header参数:

    解决方案:

    location / {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    }
     

    3、Ajax跨域请求保证同一个session的问题

    我们知道,根据浏览器的保护规则,跨域的时候我们创建的sessionId是不会被浏览器保存下来的,这样,当我们在进行跨域访问的时候,我们的sessionId就不会被保存下来,也就是说,每一次的请求,服务器就会以为是一个新的人,而不是同一个人,为了解决这样的办法,下面这种方法可以解决这种跨域的办法。

    我们自己构建一个拦截器,对需要跨域访问的request头部重写:

    add_header Access-Control-Allow-Origin "$http_origin";
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    add_header Access-Control-Allow-Headers "Origin, Accept, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token";
    add_header Access-Control-Allow-Credentials "true";
     

    原文链接:https://blog.csdn.net/weixin_42207486/java/article/details/82494638

  • 相关阅读:
    SpringBoot-14-MyBatis预热篇,MySQL小结
    SpringBoot-13-插曲之Node文件重命名+自动生成json对象
    八月十九风雨大作
    诉世书
    《仪式》
    珊瑚墓地
    新生
    《应龙》
    《枝·你是树的狂舞》
    golang中使用etcd
  • 原文地址:https://www.cnblogs.com/yueminghai/p/12890346.html
Copyright © 2011-2022 走看看