【百度地图api资源文件http请求报错】
·
起因
vite项目使用的是动态script动态加载百度api文件 上线之后加载了http链接的百度资源文件,但是需要的是https资源文件。所以报错
报错信息
Mixed Content: The page at '<URL>' was loaded over HTTPS, but requested an insecure script '<URL>'. This request has been blocked; the content must be served over HTTPS.
解决方法
1. 在index.html中的head标签内
2.
//必须在引入百度地图的链接之前声明
<script>window.HOST_TYPE = 2; window.BMAP_PROTOCOL = 'https';</script>
3. 在页面内部写入
const AK = '密钥名称' // 你的密钥
const loadBaiduMap = () => {
new Promise<typeof BMap>((resolve, reject) => {
if (window.BMap) return resolve(window.BMap)
// 1. 告诉 SDK 后续资源一律走 HTTPS
window.HOST_TYPE = 2 // 关键!
window.BMAP_PROTOCOL = 'https' // 兼容旧版(≤2.0)
// 2. 挂回调
;(window as any)._initBMap = () => resolve(window.BMap)
// 3. 拉 SDK(用 api 更简洁,也能回调)
const script = document.createElement('script')
script.src = `https://api.map.baidu.com/api?v=3.0&ak=${AK}&s=1&callback=_initBMap`
script.onerror = () => reject(new Error('百度地图 SDK 加载失败'))
document.head.appendChild(script)
})
}
更多推荐


所有评论(0)