当前位置: 代码网 > it编程>数据库>Access > nginx代理-解决CORS跨域问题: Access to XMLHttpRequest at XXX from XXX

nginx代理-解决CORS跨域问题: Access to XMLHttpRequest at XXX from XXX

2024年08月01日 Access 我要评论
先看报错信息:address=&key=&get_poi=*’ from origin ‘https://域名’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.


1.前言简介

先看报错信息:

2. nignx角度解决方案

2.1 分析问题原因

2.1.1 翻译一波

https://***源访问https://apis.map.qq.com/ws/geocoder/v1/的xmlhttprequest已被cors策略阻止:在被请求的资源上没有access- control - allow - origin头。

2.1.2 分析及解决方案

分析

解决方案:

2.2 nginx配置如下

2.2.1 增加一个代理配置

示例代码:
  location /map/v/ {  
    
        # 路径重写
        rewrite /map/v/(.*)$ /$1 break;
        proxy_pass https://apis.map.qq.com;  
         # proxy_pass https://www.baidu.com/;  
        
         #proxy_set_header host $host;  
         #proxy_set_header x-real-ip $remote_addr;  
         #proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;  
         #proxy_set_header x-forwarded-proto $scheme;  
  
        # 添加cors响应头部  
        add_header 'access-control-allow-origin' '*';  
        add_header 'access-control-allow-methods' '*';  
        add_header 'access-control-allow-headers' '*';  
        add_header 'access-control-max-age' 1728000;  
  
        # 对于预检请求(options),直接返回200  
        if ($request_method = options) {  
            return 204;  
        }  
    }  

2.2.2 使用方式

2.3 问题解决

2.3.1 改完不生效

3. 其他解决方案

前端解决
或者后端服务远程调用 服务内配置跨域(不推荐)



作者pingzhuyan 感谢观看

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com