• 请不要在回答技术问题时复制粘贴 AI 生成的内容
cnoder
V2EX  ›  程序员

nginx 加了允许跨域为什么还是会偶尔出现跨域的情况

  •  
  •   cnoder · May 23, 2019 · 4512 views
    This topic created in 2549 days ago, the information mentioned may be changed or developed.

    server {
        add_header Access-Control-Allow-Origin *;
    ...
    我就是这样加的

    偶尔会这样
    Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    Supplement 1  ·  May 24, 2019
    把这些都加上了还是会偶尔显示跨域
    add_header Access-Control-Allow-Origin *;<br/>
    add_header 'Access-Control-Allow-Credentials' 'true';<br/>
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';<br/>
    add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    8 replies    2019-05-24 09:55:57 +08:00
    yoshiyuki
        1
    yoshiyuki  
       May 23, 2019
    CORS 分简单和复杂两种情况
    要么加了额外 header,要么带了 cookie,要么使用了 GET/POST 以外的 method,请自查
    si
        2
    si  
       May 23, 2019
    复杂请求还要做其他处理,详细情况你可以看看这个文章。
    http://www.ruanyifeng.com/blog/2016/04/cors.html
    Danfi
        3
    Danfi  
       May 23, 2019   ❤️ 1
    可能你的服务返回了 4**,5**等状态码
    Sparetire
        4
    Sparetire  
       May 23, 2019 via Android
    CORS 是很多个头部配合使用的。。复杂情况这一个头部不够的
    Takamine
        5
    Takamine  
       May 23, 2019 via Android
    这样不会不安全吗。_(:з」∠)_
    ericgui
        6
    ericgui  
       May 24, 2019
    大概率是 Chrome 的锅
    因为 chrome 会在某些时候给个 CORS error,实际上这是出于安全考虑,不给你发送实际的 error message

    你应该用 postman 来查看真实的 error message
    cnoder
        7
    cnoder  
    OP
       May 24, 2019
    @Sparetire
    @yoshiyuki
    已经加上附言内的 header 了
    yoshiyuki
        8
    yoshiyuki  
       May 24, 2019
    @cnoder 参考 6#,试试其他浏览器能否复现,判断锅的位置
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2902 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 13:39 · PVG 21:39 · LAX 06:39 · JFK 09:39
    ♥ Do have faith in what you're doing.