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

qv7cva1a  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(92)

在开发环境中,我不想通过使用https and SSL/STL certifications来使东西成为complicated
我学习了如何防止Google Chrome from redirecting localhost to https,在Chrome地址栏中输入http:/localhost:105/hello/后返回一个JSON对象。
令我困惑的是,以下内容在浏览器控制台(检查->控制台)中对https页面不起作用:

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
Uncaught(in promise)TypeError:无法获取
在:1:1
在fetch方法中添加参数{redirect: 'manual'}没有帮助。
感谢@novavanity。它可以在URL为http://localhost:105/hello/的页面上工作
我不知道为什么以及如何将http重定向到https?我怎么能阻止呢?

xv8emn3q

xv8emn3q1#

有时,从HTTP到HTTPS的重定向是由于307错误代码(内部重定向)。你可以在Chrome开发者工具面板的“网络”选项卡中查看。
Where the network tab is located
因此,如果您之前将URL设置为HTTPS,则可能需要清除Chrome上的缓存并重新启动应用程序。希望能帮上忙。

3okqufwl

3okqufwl2#

当您在浏览器中使用fetch()时,如果当前页面通过HTTPS加载,则会自动使用HTTPS协议。这可能就是为什么你会得到
ERR_SSL_PROTOCOL_ERROR
错误当尝试访问http://localhost:105/hello/使用fetch().
要阻止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))

相关问题