简体   繁体   English

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

[英]fetch localhost without redirecting to https?

In the development environment, I don't want to make things complicated by using https and SSL/STL certifications .在开发环境中,我不想因为使用https 和 SSL/STL 认证而使事情变得复杂

I learned how to prevent Google Chrome from redirecting localhost to https , and a JSON object returned after entering http:/localhost:105/hello/ into the Chrome address bar.我了解了如何防止Google Chrome 将本地主机重定向到 https ,并在 Chrome 地址栏中输入http:/localhost:105/hello/后返回 JSON object。

What confused me is that the following does not work in the browser console(Inspect->Console) for https pages:让我感到困惑的是,以下内容在 https 页面的浏览器控制台(Inspect->Console)中不起作用:

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

The error reads that:错误内容如下:

GET https://localhost:105/hello/.net::ERR_SSL_PROTOCOL_ERROR获取 https://localhost:105/hello/.net::ERR_SSL_PROTOCOL_ERROR
Uncaught (in promise) TypeError: Failed to fetch未捕获(承诺)TypeError:无法获取
at:1:1在:1:1

And adding parameter {redirect: 'manual'} in the fetch method doesn't help.在 fetch 方法中添加参数{redirect: 'manual'}没有帮助。

Thanks to @novavanity .感谢@novavanity It does work on the page with the url http://localhost:105/hello/它确实在页面上工作 url http://localhost:105/hello/

I don't know why and how the http was redirected to https?我不知道为什么以及如何将 http 重定向到 https? How can I prevent that?我怎样才能防止这种情况发生?

When you use fetch() in the browser, it automatically uses the HTTPS protocol if the current page is loaded over HTTPS. This is probably why you are getting the当您在浏览器中使用fetch()时,如果当前页面通过 HTTPS 加载,它会自动使用 HTTPS 协议。这可能就是您获得

ERR_SSL_PROTOCOL_ERROR ERR_SSL_PROTOCOL_ERROR

error when trying to access http://localhost:105/hello/ using fetch() .尝试使用fetch()访问 http://localhost:105/hello/ 时出错。

To prevent fetch() from using HTTPS, you can use the base option in the Request object that you pass to fetch() :要防止fetch()使用 HTTPS,您可以在传递给fetch()的请求 object 中使用 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不重定向的情况下获取 PHP 脚本? - How to fetch PHP script without redirecting? JavaScript GetUserMedia 在没有 HTTPS 的情况下使用带有本地主机的 Chrome - JavaScript GetUserMedia using Chrome with localhost without HTTPS React Native fetch https localhost网络请求失败 - React Native fetch https localhost network request failed CORS 策略已阻止从源“http://localhost:3000”访问“https://localhost:7144/api/employees” - Access to fetch at 'https://localhost:7144/api/employees' from origin 'http://localhost:3000' has been blocked by CORS policy CORS 策略已阻止从来源“null”访问“https://localhost:44395” - Access to fetch at 'https://localhost:44395' from origin 'null' has been blocked by CORS policy 需要帮助使用 fetch 重定向 - need help redirecting with fetch 将 localhost:3000 重定向到 localhost:3000/newpage - Redirecting localhost:3000 to localhost:3000/newpage 从本地主机重定向到HTML页面 - Redirecting to a html page from localhost 如何从不同的网站获取 pdf 文件并将其显示在我们的网站上而无需重定向? - how to fetch a pdf file from a different website and display it in our website without redirecting? 网站未从https重定向到http - Site not redirecting to http from https
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM