为什么我的https://请求取消并更改为http://,导致混合请求错误

Why is my https:// request cancelled and changed to http://, causing a mixed request error?

本文关键字:请求 http 错误 混合 https 我的 取消 为什么      更新时间:2023-09-26

我有一些简单的延迟加载javascript:

script.type = 'text/javascript';
script.src = 'https://example.com/'+urlToJavascript;
script.onreadystatechange = callback;
script.onload = callback;   
head.appendChild(script);

SSL在我的域名https://example.com/上运行。尽管https在剧本里。src我得到这个混合内容错误:

The page at 'https://example.com/' was loaded over HTTPS, but requested an insecure script 'http://example.com/js/lazyScript.js'. This request has been blocked; the content must be served over HTTPS.

我运行console.log(script),显示:

<script type="text/javascript" src="https://example.com/js/lazyScript.js"></script>

然后我转到检查器的网络选项卡,发现对https://example.com/js/lazyScript.js的初始请求被提出,但随后被取消,其次是对不安全的http://example.com/js/lazyScript.js的请求,然后由于混合内容而被阻止。

我以前从未遇到过这种情况,也不知道为什么会发生这种情况。

为什么会这样?

这可能是由于服务器级的一些重写。我也面临同样的问题,它是nginx以某种方式将url转换回http。尝试直接在浏览器中加载资源url,看看是否有重定向发生。如果是,返回服务器并修复重定向。