From f392ae318d7d36ef0e145dca97e8ee60715931ae Mon Sep 17 00:00:00 2001 From: wsdfm <125838816@qq.com> Date: Wed, 24 Jul 2019 17:57:05 +0800 Subject: [PATCH] done --- README.md | 38 +++++++++++++++++++++++++++++++++++++- cors.js | 17 +++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 cors.js diff --git a/README.md b/README.md index fcc0487..0fbbf81 100644 --- a/README.md +++ b/README.md @@ -1 +1,37 @@ -# homework-browser-network-final \ No newline at end of file +# 列举 CORS 头信息,并说明⽤用途 + +## Access-Control-Allow-Origin +>Access-Control-Allow-Origin: \ | * + +这个头部信息由服务器返回,用来明确指定那些客户端的域名允许访问这个资源。 + +对于不需要携带身份凭证的请求,服务器可以指定该字段的值为通配符,表示允许来自所有域的请求f。 + +## Access-Control-Expose-Headers +>Access-Control-Expose-Headers: X-My-Custom-Header, X-Another-Custom-Header + +在跨域访问时,XMLHttpRequest对象的getResponseHeader()方法只能拿到一些最基本的响应头,Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma,如果要访问其他头,则需要服务器设置本响应头。 + +Access-Control-Expose-Headers 头让服务器把允许浏览器访问的头放入白名单。 +## Access-Control-Max-Age +Access-Control-Max-Age 头指定了preflight请求的结果能够被缓存多久,请参考本文在前面提到的preflight例子。 + +>Access-Control-Max-Age: + +delta-seconds 参数表示preflight请求的结果在多少秒内有效。 + +## Access-Control-Allow-Credentials +这个头部信息只会在服务器支持通过cookies传递验证信息的返回数据里。它的值只有一个就是 true。 + +>Access-Control-Allow-Credentials: true + +## Access-Control-Allow-Methods +Access-Control-Allow-Methods 首部字段用于预检请求的响应。其指明了实际请求所允许使用的 HTTP 方法。 + +Access-Control-Allow-Methods: \[, \]* + + +## Access-Control-Allow-Headers +Access-Control-Allow-Headers 首部字段用于预检请求的响应。其指明了实际请求中允许携带的首部字段。 + +Access-Control-Allow-Headers: \[, \]* \ No newline at end of file diff --git a/cors.js b/cors.js new file mode 100644 index 0000000..deeec4c --- /dev/null +++ b/cors.js @@ -0,0 +1,17 @@ +// 带上cookie,失败后重选请求 +function fetchData(url, method) { + let i = 1 + return function request(url, method) { + fetch(url, { + method: method, + credentials: 'include' + }) + .then(res => res.json()) + .catch(err => { + if (++i <= 5) { + return request(url, method) + } + console.log('done') + }) + }() +} \ No newline at end of file