在开发环境中,我不想使用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?我怎样才能防止这种情况发生?
当您在浏览器中使用
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))