最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript fetch automatically changes http with https - Stack Overflow

programmeradmin5浏览0评论

I am making a request with fetch at the client side with this code:

    var request = new Request(`http://ip:8080/click?url=${value}`, {
        method: 'GET',
        headers: new Headers({
            "Content-Type": "application/json"
        }),
    });
    fetch(request)

but when the browser makes the request, it automatically changes the URL with https protocol:

https://ip:8080/click?url=${value}

Note: the webpage has SSL encryption

Subsequently, I get this error on the webpage console: Failed to load resource: net::ERR_CONNECTION_CLOSED

I am making a request with fetch at the client side with this code:

    var request = new Request(`http://ip:8080/click?url=${value}`, {
        method: 'GET',
        headers: new Headers({
            "Content-Type": "application/json"
        }),
    });
    fetch(request)

but when the browser makes the request, it automatically changes the URL with https protocol:

https://ip:8080/click?url=${value}

Note: the webpage has SSL encryption

Subsequently, I get this error on the webpage console: Failed to load resource: net::ERR_CONNECTION_CLOSED

Share Improve this question edited Mar 16, 2022 at 9:42 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Sep 22, 2020 at 17:25 ctbctb 1711 gold badge1 silver badge4 bronze badges 6
  • The server doesn't support https requests, when I'm testing the webpage on my local browser I don't have any problem, but when doing it on the webpage with SSL encryption then the error happens. – ctb Commented Sep 22, 2020 at 17:28
  • What ?! you write in the ment the server "dosn't support https" and in your post, "the webpage has ssl encryption" ?! – Marc Commented Sep 22, 2020 at 17:35
  • yes, the webpage hosting has SSL encryption. The server where the API resides doesn't support https requests – ctb Commented Sep 22, 2020 at 17:41
  • I think your problem is CORS: en.wikipedia/wiki/Cross-origin_resource_sharing – Marc Commented Sep 23, 2020 at 6:18
  • @Marc How can it be a CORS problem since the changes from http to https is done by the browser? – Karol Borkowski Commented Jun 30, 2021 at 10:53
 |  Show 1 more ment

1 Answer 1

Reset to default 13

I had the same problem and the reason was the following line in the head section:

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

It replaces every http requests with https.

发布评论

评论列表(0)

  1. 暂无评论