Javascript 如何处理 net::ERR_SSL_PROTOCOL_ERROR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43232686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deal with net::ERR_SSL_PROTOCOL_ERROR?
提问by BB-8
I am including a JS library into my website
我在我的网站中包含了一个 JS 库
<script type="text/javascript" src="http://www.turnjs.com/lib/turn.min.js "></script>
that is designed, maintained and hosted by a webdesign company. The implementation of what I am trying to achieve seems to be well coded – see my JSFiddle– it works fine with no errors. However when a copy and paste the code from my JSfiddle into my website, it doesn't work at all – Google Chrome developer console shows this:
由网页设计公司设计、维护和托管。我试图实现的实现似乎编码良好 - 请参阅我的JSFiddle- 它运行良好,没有错误。但是,当将我的 JSfiddle 中的代码复制并粘贴到我的网站时,它根本不起作用 – Google Chrome 开发者控制台显示:
GET https://www.turnjs.com/lib/turn.min.js net::ERR_SSL_PROTOCOL_ERROR
GET https://www.turnjs.com/lib/turn.min.js net::ERR_SSL_PROTOCOL_ERROR
But it is weird, since my Jsfiddle code is including the same turn.min.jsand it works there, but on my website it doesn't.
但这很奇怪,因为我的 Jsfiddle 代码包含相同的代码turn.min.js并且它在那里工作,但在我的网站上却没有。
回答by Franco Bianconi
This is not a javascript problem, it's an ssl probem.
这不是 javascript 问题,而是 ssl 问题。
You can't get a file through an insecure connection (http) from a html page served through a secure connection (https). If you're going to use https (and you SHOULD!) then you need to get the script through a secure connection. To get the file, you should use:
您无法从通过安全连接 (https) 提供的 html 页面通过不安全连接 (http) 获取文件。如果您打算使用 https(并且您应该!),那么您需要通过安全连接获取脚本。要获取文件,您应该使用:
<script type="text/javascript" src="https://www.turnjs.com/lib/turn.min.js "></script>
And make sure that the file is being served through https, by configuring the server accordingly. Here's a simple configuration for lighttpd, and here's one for apache. If you don't have a CA signed certificate, you could get one with letsencrypt ( www.letsencrypt.org/getting-started/ ). If you don't control the server, you should get in contact with the person who does.
并通过相应地配置服务器来确保通过 https 提供文件。这是lighttpd的一个简单配置,这是apache的一个。如果您没有 CA 签名证书,您可以通过letsencrypt ( www.letsencrypt.org/getting-started/ ) 获得一个。如果您不控制服务器,您应该与控制服务器的人取得联系。
回答by SouXin
- 1) http://www.turnjs.com/lib/turn.min.jscould not be reached through HTTPS. This causes your error.
- 2) JSfiddle accessed through HTTP hence no problems
- 3)
<script type="text/javascript" src="http://www.turnjs.com/lib/turn.min.js "></script>will cause problems in any cases for your site, because this is an example of mixed content which will be blocked by almost all browsers.
- 1) http://www.turnjs.com/lib/turn.min.js无法通过 HTTPS 访问。这会导致您的错误。
- 2)JSfiddle通过HTTP访问因此没有问题
- 3)
<script type="text/javascript" src="http://www.turnjs.com/lib/turn.min.js "></script>在任何情况下都会对您的网站造成问题,因为这是几乎所有浏览器都会阻止的混合内容示例。
Solution:
解决方案:
Copy this file on your server and then include local version
将此文件复制到您的服务器上,然后包含本地版本
I prefer include script to the pages in that way:
<script type="text/javascript" src="//www.turnjs.com/lib/turn.min.js "></script>.
我更喜欢以这种方式在页面中包含脚本:
<script type="text/javascript" src="//www.turnjs.com/lib/turn.min.js "></script>.
That allowed browser to check connection type itself. Which is actually won't work in you case (see p.1).
这允许浏览器自行检查连接类型。这在你的情况下实际上是行不通的(见第 1 页)。
An very good explanation in Google developers
谷歌开发者的一个很好的解释

