获取本地主机而不重定向到 https?

问题描述 投票:0回答:2

在开发环境中,我不想使用https和SSL/STL认证使事情变得复杂

我学会了如何防止 Google Chrome 将本地主机重定向到 https,并在 Chrome 地址栏中输入

http:/localhost:105/hello/
后返回一个 JSON 对象。

令我困惑的是,以下内容在 https 页面的浏览器控制台(Inspect->Console)中不起作用:

const url = new URL('http://localhost:105/hello/');
fetch(url).then(response => response.json()).then(json => console.log(json))

错误是这样写的:

获取 https://localhost:105/hello/ net::ERR_SSL_PROTOCOL_ERROR
未捕获(承诺)TypeError:无法获取
在:1:1

在 fetch 方法中添加参数

{redirect: 'manual'}
没有帮助。

感谢@novavanity。它确实在 url http://localhost:105/hello/

的页面上工作

我不知道为什么以及如何将http重定向到https?我怎样才能防止这种情况发生?

javascript http redirect browser https
2个回答
0
投票

有时,从 HTTP 到 HTTPS 的重定向是由于 307 错误代码(内部重定向)而发生的。您可以在 Chrome 开发者控制台的“网络”选项卡中查看。

Where the network tab is located

因此,如果您之前的 URL 为 HTTPS,您可能需要清除 Chrome 上的缓存并重新启动应用程序。希望有所帮助。


-1
投票

当您在浏览器中使用

fetch()
时,如果当前页面是通过HTTPS 加载的,它会自动使用HTTPS 协议。这可能就是你得到

的原因

ERR_SSL_PROTOCOL_ERROR

尝试使用

fetch()
访问 http://localhost:105/hello/ 时出错。

为了防止

fetch()
使用HTTPS,您可以在传递给
fetch()
的Request对象中使用base选项:

const url = new URL('http://localhost:105/hello/');
const options = {
  base: 'http://localhost:105/'
};

fetch(url, options).then(response => response.json()).then(json => console.log(json))
© www.soinside.com 2019 - 2024. All rights reserved.