aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/fetch_api/cross-global_fetch_usage/index.html
blob: 7ca474a67a137398fca3f971fb21c935e40df2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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>