Fetch API 默认跨源行为
Fetch API default cross-origin behavior
Fetch Specifications 表示默认的 Fetch 模式是 'no-cors' -
A request has an associated mode, which is "same-origin", "cors", "no-cors", "navigate", or "websocket". Unless stated otherwise, it is "no-cors".
但是,我似乎注意到 mode: 'no-cors'
和未指定模式之间的这种行为差异。如本 JSFiddle sample 中所示,将模式显式定义为 'no-cors' 会使 Javascript 无法访问响应,而不指定模式会使 Response 对象可供调用方法使用。
显式指定获取模式的工作方式是否与内部使用相同模式的默认行为不同?我在这里错过了什么?
no-cors
只是基本 Fetch 算法的默认值。但是在规范的其他不同地方,该算法被调用时使用了不同于 no-cors
的特定模式。特别是,对于跨域请求,该算法在模式设置为 cors
.
的情况下被调用
更具体地说,对于跨源请求,Fetch 算法设置 “response tainting” for the request to cors
and requires the fetch to be performed using the CORS protocol。
更具体地说,请参阅 “Main fetch” algorithm 步骤 12 的以下子步骤:
↪ request’s current url’s origin is same origin with request’s
origin and CORS flag is unset
↪ request’s current url’s scheme is "data
"
↪ request’s mode is "navigate
" or "websocket
"
- Set request’s response tainting to "
basic
".
- Return the result of performing a scheme fetch using request.
…
↪ request’s mode is "no-cors
"
- Set request’s response tainting to "
opaque
".
- Return the result of performing a scheme fetch using request.
…
↪ Otherwise
- 将请求的响应污染设置为“
cors
”。
- Return 使用设置了 CORS 标志 的请求执行 HTTP 提取的结果。
也就是说,如果请求 URL 与您的代码来源不同源,则方案不是 data
,模式也不是 [=15] =] 或 websocket
或未明确设置为 no-cors
,则达到 Otherwise 条件,并使用 CORS protocol 发出请求.
这个解释稍微简化了事情,因为在请求具有触发浏览器进行预检的属性的情况下,在该 Otherwise 子步骤之上有一个子步骤相反——但在那种情况下,“响应污染”被设置为 cors
.
But, I seem to be noticing this behavioral difference between mode: 'no-cors'
and an unspecified mode.
不同之处在于,当您为请求显式设置 no-cors
模式时,在这种情况下 “↪ 请求的模式为“no-cors”” 子步骤得到达到而不是 Otherwise 子步骤。
As demonstrated in this JSFiddle sample, explicitly defining mode as 'no-cors' makes the response inaccessible to the Javascript,
对——那是因为它导致 “↪ 请求的模式是“no-cors”” 到达“主获取”算法中的子步骤而不是 否则子步骤。
while not specifying a mode makes the Response object available to the calling method.
因为 Otherwise 子步骤随后会到达,所以会进行启用 CORS 的提取。
Does explicitly specifying the fetch mode work differently from the default behavior that internally uses the same mode?
是的,对于跨源请求,显式设置 mode: 'no-cors'
会强制在不使用 CORS 的情况下发出请求,而未指定则会导致使用 CORS 发出请求。
“除非另有说明,否则它是“no-cors
”” 关于模式的声明绝对不是要指定使用 [= =30=] 没有指定显式模式的方法最终将其模式锁定为 no-cors
.
Fetch Specifications 表示默认的 Fetch 模式是 'no-cors' -
A request has an associated mode, which is "same-origin", "cors", "no-cors", "navigate", or "websocket". Unless stated otherwise, it is "no-cors".
但是,我似乎注意到 mode: 'no-cors'
和未指定模式之间的这种行为差异。如本 JSFiddle sample 中所示,将模式显式定义为 'no-cors' 会使 Javascript 无法访问响应,而不指定模式会使 Response 对象可供调用方法使用。
显式指定获取模式的工作方式是否与内部使用相同模式的默认行为不同?我在这里错过了什么?
no-cors
只是基本 Fetch 算法的默认值。但是在规范的其他不同地方,该算法被调用时使用了不同于 no-cors
的特定模式。特别是,对于跨域请求,该算法在模式设置为 cors
.
更具体地说,对于跨源请求,Fetch 算法设置 “response tainting” for the request to cors
and requires the fetch to be performed using the CORS protocol。
更具体地说,请参阅 “Main fetch” algorithm 步骤 12 的以下子步骤:
↪ request’s current url’s origin is same origin with request’s origin and CORS flag is unset
↪ request’s current url’s scheme is "data
"
↪ request’s mode is "navigate
" or "websocket
"
- Set request’s response tainting to "
basic
".- Return the result of performing a scheme fetch using request.
…
↪ request’s mode is "no-cors
"- Set request’s response tainting to "
opaque
".- Return the result of performing a scheme fetch using request.
…
↪ Otherwise
- 将请求的响应污染设置为“
cors
”。 - Return 使用设置了 CORS 标志 的请求执行 HTTP 提取的结果。
也就是说,如果请求 URL 与您的代码来源不同源,则方案不是 data
,模式也不是 [=15] =] 或 websocket
或未明确设置为 no-cors
,则达到 Otherwise 条件,并使用 CORS protocol 发出请求.
这个解释稍微简化了事情,因为在请求具有触发浏览器进行预检的属性的情况下,在该 Otherwise 子步骤之上有一个子步骤相反——但在那种情况下,“响应污染”被设置为 cors
.
But, I seem to be noticing this behavioral difference between
mode: 'no-cors'
and an unspecified mode.
不同之处在于,当您为请求显式设置 no-cors
模式时,在这种情况下 “↪ 请求的模式为“no-cors”” 子步骤得到达到而不是 Otherwise 子步骤。
As demonstrated in this JSFiddle sample, explicitly defining mode as 'no-cors' makes the response inaccessible to the Javascript,
对——那是因为它导致 “↪ 请求的模式是“no-cors”” 到达“主获取”算法中的子步骤而不是 否则子步骤。
while not specifying a mode makes the Response object available to the calling method.
因为 Otherwise 子步骤随后会到达,所以会进行启用 CORS 的提取。
Does explicitly specifying the fetch mode work differently from the default behavior that internally uses the same mode?
是的,对于跨源请求,显式设置 mode: 'no-cors'
会强制在不使用 CORS 的情况下发出请求,而未指定则会导致使用 CORS 发出请求。
“除非另有说明,否则它是“no-cors
”” 关于模式的声明绝对不是要指定使用 [= =30=] 没有指定显式模式的方法最终将其模式锁定为 no-cors
.