发布上线后百度地图不显示Refused to load the script 'https://api.map.baidu.com/api?v=3.0&ak=' because it violates the following Content Security Policy directive:
标题翻译之后是这样的
拒绝加载脚本'https://api.map.baidu.com/api?v=3.0&ak=秘钥',因为它违反了以下内容安全策略指令:"default-src 'self' 'unsafe-inline' 'unsafe-eval'"。注意,'script-src-elem'没有显式设置,所以'default-src'被用作备用。
原因就是在浏览器拒绝了外部注入的脚本<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=秘钥"></script>
查看其他接口的相应头可以看到有这个东西:
Content-Security-Policy:default-src 'self' 'unsafe-inline' 'unsafe-eval' ;img-src 'self' data:; frame-src *; font-src *;
其中 default-src 'self' 就是表示只允许加载本域下的脚本。
这样设置的原因是减少xss(跨站脚本攻击)的发生。
解决办法:可以和后端沟通一下,设置Content-Security-Policy 允许可以要加载的外部脚本 add_header Content-Security-Policy "script-src 'self' https://webapi.amap.com 'unsafe-inline' 'unsafe-eval' blob: data:;";
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CSP