Skip to content

广州-肖业翔-cors-retry #3

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 2 commits 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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# homework-browser-network-final
# homework-browser-network-final


## 列列举 CORS 的头信息,并说明⽤用途

#### Access-Control-Allow-Origin
该字段是必须的。它的值要么是请求时 ```Origin``` 字段的值,要么是一个 ```*```,表示接受任意域名的请求。

#### Access-Control-Allow-Credentials
该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。设为 ```true```,即表示服务器明确许可,Cookie可以包含在请求中,一起发给服务器。这个值也只能设为 ```true```,如果服务器不要浏览器发送Cookie,删除该字段即可。

#### Access-Control-Expose-Headers
该字段可选。CORS请求时,```XMLHttpRequest``` 对象的```getResponseHeader()``` 方法只能拿到6个基本字段:```Cache-Control```、```Content-Language```、```Content-Type```、```Expires```、```Last-Modified```、```Pragma```。如果想拿到其他字段,就必须在```Access-Control-Expose-Headers```里面指定。上面的例子指定,getResponseHeader('FooBar')可以返回FooBar字段的值。


需要注意的是,如果要发送Cookie,```Access-Control-Allow-Origin``` 就不能设为星号,必须指定明确的、与请求网页一致的域名。同时,Cookie依然遵循同源政策,只有用服务器域名设置的Cookie才会上传,其他域名的Cookie并不会上传,且(跨源)原网页代码中的 ```document.cookie``` 也无法读取服务器域名下的Cookie。(**巨坑,我正式项目背坑过**)
71 changes: 71 additions & 0 deletions cors-retry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
((function() {
'use strict';

// CORS 跨域请求带上 cookie
function corsWithCooies(url) {
var corsWithCooiesHttp = new XMLHttpRequest();
corsWithCooiesHttp.open('GET', url);
corsWithCooiesHttp.withCredentials = true;
corsWithCooiesHttp.onreadystatechange = function() {
if (corsWithCooiesHttp.readyState == 4 && corsWithCooiesHttp.status == 200) {
console.log(corsWithCooiesHttp.responseText);
}
};
corsWithCooiesHttp.send();
}

/**
* retry 写⼀段让 http 请求失败后重试的代码
* @desc 必须返回一个承诺
* @param {Function} func 重试函数
* @param {Number} [times=10] 最多重试次数
* @param {Number} [timeout=100] 延迟重试时间
* @return {Function} function
*/
function retry(func, times = 10, timeout = 100) {
return function(...args) {
return new Promise((resolve, reject) => {
function tryFunc(...args) {
Promise.resolve()
.then(() => func.apply(this, args))
.then(result => resolve(result))
.catch(error => {
times--;
if (times > 0) {
setTimeout(() => tryFunc.apply(this, args), timeout);
} else {
reject(error);
}
});
}

tryFunc.apply(this, args);
});
}
}

var times = 0;

function test(text) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(text);

times++;
if (times < 5) {
reject('failed');
} else {
resolve('success');
}
}, 500)
})
}

var testRetry = retry(test, 4, 100);

testRetry('try')
.then(data => console.log(data))
.catch(error => console.error('all retries failed'));


})())