Skip to content

http失败重试与CORS携带Cookie--余玉玲 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# homework-browser-network-final
# homework-browser-network-final

## CORS 头信息

### HTTP 响应首部字段

`Access-Control-Allow-Origin`: 指定了允许访问该资源的外域 URI

`Access-Control-Expose-Headers`: 让服务器把允许浏览器访问的头放入白名单

`Access-Control-Max-Age`: 指定了preflight请求的结果能够被缓存多久

`Access-Control-Allow-Credentials`: 指定了当浏览器的credentials设置为true时是否允许浏览器读取response的内容。

`Access-Control-Allow-Methods`: 用于预检请求的响应。其指明了实际请求所允许使用的 HTTP 方法。

`Access-Control-Allow-Headers`: 用于预检请求的响应。其指明了实际请求中允许携带的首部字段。

### HTTP 请求首部字段

`Origin`: 表明预检请求或实际请求的源站

`Access-Control-Request-Method`: 用于预检请求。其作用是,将实际请求所使用的 HTTP 方法告诉服务器。

`Access-Control-Request-Headers`: 用于预检请求。其作用是,将实际请求所携带的首部字段告诉服务器。
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>CORS</title>
</html>
<body>
<script src="./index.js"></script>
</body>
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// CORS 带上 cookie
function getData(url) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.withCredentials = true;
xhr.send();
}

// http请求失败后重试
async function reconnect(func, interval, frequency) {
try {
await func();
} catch {
if (frequency > 0 ) {
frequency -= 1;
setTimeout(function() {
reconnect(func, interval, frequency);
}, interval);
}
}
}