https协议中自动升级http协议 -> https
一、前言
最近在项目中发现在https中调用http接口会失败,原因是https的限制
解决办法如下
- 根据web服务器的协议自动升级http请求
// https协议自动升级http请求到https
if (window.location.protocol === 'https:') {
const httpsMeta = document.createElement("meta")
httpsMeta.setAttribute("http-equiv", 'Content-Security-Policy')
httpsMeta.setAttribute("content", 'upgrade-insecure-requests')
document.head.insertBefore(httpsMeta, document.head.firstChild);
}