Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

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 请求

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

你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有问题。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...