javascript - HTTPS请求被浏览器取消,并 被视作HTTP请求

查看:164
本文介绍了javascript - HTTPS请求被浏览器取消,并 被视作HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

    fetch('https://vanishingdante.github.io/gh-pages-blog-api/2017/02/11/bye-world')
      .then(resp => resp.json())
      .then(json => console.log(json))

我用 fetch api 尝试从我的 gh-pages 获取 json 数据 (gh-pages Access-Control-Allow-Origi 的 为 *)

但是我得到的是这个

然后我检查了一下 Chrome devtool 的 Network

我的 HTTPS 请求被浏览器取消,并且该次请求被视作一次 HTTP 请求

为什么会这样,怎么处理这种情况

解决方案

你fetch的这个地址虽然是https的,但是里面是301跳转,又跳回http了。

[root@localhost ~]# curl https://vanishingdante.github.io/gh-pages-blog-api/2017/02/11/bye-world -i
HTTP/1.1 301 Moved Permanently
Server: GitHub.com
Content-Type: text/html
Location: http://vanishingdante.github.io/gh-pages-blog-api/2017/02/11/bye-world/
Access-Control-Allow-Origin: *
Expires: Mon, 20 Feb 2017 03:31:30 GMT
Cache-Control: max-age=600
X-GitHub-Request-Id: DCFE:28E4:64D5D4E:8722232:58AA60B9
Content-Length: 178
Accept-Ranges: bytes
Date: Mon, 20 Feb 2017 03:21:34 GMT
Via: 1.1 varnish
Age: 4
Connection: keep-alive
X-Served-By: cache-itm7424-ITM
X-Cache: HIT
X-Cache-Hits: 1
X-Timer: S1487560894.032116,VS0,VE0
Vary: Accept-Encoding
X-Fastly-Request-ID: e55e5f1de288a19b7772c1ac3902ce61bd121201

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

不仅如此,尝试再去请求这个http地址,又会跳转到https:

[root@localhost ~]# curl http://vanishingdante.github.io/gh-pages-blog-api/2017/02/11/bye-world -i 
HTTP/1.1 301 Moved Permanently
Server: GitHub.com
Content-Type: text/html
Location: https://vanishingdante.github.io/gh-pages-blog-api/2017/02/11/bye-world
X-GitHub-Request-Id: A1F0:61E6:4D0C0CE:60DEE17:58AA6105
Content-Length: 178
Accept-Ranges: bytes
Date: Mon, 20 Feb 2017 03:22:45 GMT
Via: 1.1 varnish
Age: 0
Connection: keep-alive
X-Served-By: cache-itm7420-ITM
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1487560965.336320,VS0,VE191
Vary: Accept-Encoding
X-Fastly-Request-ID: f1d26de898e81b14cf0dacbe28ad6dc716fca747

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

你的gh-pages有问题。

这篇关于javascript - HTTPS请求被浏览器取消,并 被视作HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆