diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/fetch_api/cross-global_fetch_usage | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/fetch_api/cross-global_fetch_usage')
-rw-r--r-- | files/zh-cn/web/api/fetch_api/cross-global_fetch_usage/index.html | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/fetch_api/cross-global_fetch_usage/index.html b/files/zh-cn/web/api/fetch_api/cross-global_fetch_usage/index.html new file mode 100644 index 0000000000..7ca474a67a --- /dev/null +++ b/files/zh-cn/web/api/fetch_api/cross-global_fetch_usage/index.html @@ -0,0 +1,38 @@ +--- +title: Cross-global fetch usage +slug: Web/API/Fetch_API/Cross-global_fetch_usage +tags: + - Fetch + - 相对 URL + - 跨源 + - 边缘情况 +translation_of: Web/API/Fetch_API/Cross-global_fetch_usage +--- +<p class="summary">本文解释了在fetch时发生的边缘情况(以及潜在的其他APIs展示相同类型的资源检索行为)。当从“iframe”发起包含相对url的跨源fetch时,相对url用于针对当前全局位置而不是iframe的位置进行解析。</p> + +<h2 id="边缘情况">边缘情况</h2> + +<p>大多数网站几乎不会遇到这种边缘情况。如下:</p> + +<ul> + <li>需要一个同源的 iframe</li> + <li>该同源 iframe 需要具有不同基址位置</li> + <li>必须使用跨全局的 fetch 函数, 例如. <code>frame.contentWindow.fetch()</code></li> + <li>传递给 fetch 函数的是相对 URL</li> +</ul> + +<h2 id="遇到的问题">遇到的问题</h2> + +<p>以前,我们从当前全局 URL 中解析相对 URL,例如:</p> + +<pre class="brush: js">let absolute = new URL(relative, window.location.href)</pre> + +<p>这样做不是什么大问题,<span id="result_box" lang="zh-CN"><span>只是表现出这种行为的不同 API 与规范中定义的行为的不一致可能导致问题的进一步发展。</span></span></p> + +<h2 id="解决方案">解决方案</h2> + +<p>在 Firefox 60 及以后版本中,Mozilla 对相对 URL 的解析是相对于拥有<code>fetch()</code>函数的全局的。(见 {{bug(1432272)}})。因此在上述情形中,URL 是相对于 iframe 的地址进行解析的:</p> + +<pre class="brush: js">let absolute = new URL(relative, frame.contentWindow.location.href)</pre> + +<p><span id="result_box" lang="zh-CN"><span>关于使新规范与此行为变化保持一致,以缓解未来可能出现的问题,大量讨论</span></span><span lang="zh-CN"><span>正在进行中。</span></span></p> |