diff options
Diffstat (limited to 'files/zh-cn/web/api/request')
-rw-r--r-- | files/zh-cn/web/api/request/cache/index.html | 103 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/clone/index.html | 62 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/context/index.html | 43 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/credentials/index.html | 64 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/headers/index.html | 151 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/index.html | 170 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/method/index.html | 113 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/mode/index.html | 80 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/request/index.html | 158 | ||||
-rw-r--r-- | files/zh-cn/web/api/request/url/index.html | 113 |
10 files changed, 1057 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/request/cache/index.html b/files/zh-cn/web/api/request/cache/index.html new file mode 100644 index 0000000000..9cf3212519 --- /dev/null +++ b/files/zh-cn/web/api/request/cache/index.html @@ -0,0 +1,103 @@ +--- +title: Request.cache +slug: Web/API/Request/cache +translation_of: Web/API/Request/cache +--- +<div>{{APIRef("Fetch")}}</div> + +<p><strong><code>cache</code></strong> 作为{{domxref("Request")}} 接口只读属性包含着请求的缓存模式。它控制着请求以何种方式与浏览器的 <a href="/en-US/docs/Web/HTTP/Caching">HTTP </a>缓存进行交互。</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="brush: js">var currentCacheMode = request.cache;</pre> + +<h3 id="Value">Value</h3> + +<p>A <code>RequestCache</code> value. The available values are:</p> + +<ul> + <li><code>default</code> — 浏览器从HTTP缓存中寻找匹配的请求。 + + <ul> + <li>如果缓存匹配上并且有效( <a href="/en-US/docs/Web/HTTP/Caching#Freshness">fresh</a>), 它将直接从缓存中返回资源。</li> + <li>如果缓存匹配上但已经过期 ,浏览器将会使用传统( <a href="/en-US/docs/Web/HTTP/Conditional_requests">conditional request</a> )的请求方式去访问远程服务器 。如果服务器端显示资源没有改动,它将从缓存中返回资源。否则,如果服务器显示资源变动,那么重新从服务器下载资源更新缓存。</li> + <li>如果缓存没有匹配,浏览器将会以普通方式请求,并且更新已经下载的资源缓存。</li> + </ul> + </li> + <li><code>no-store</code> — 浏览器直接从远程服务器获取资源,不查看缓存,并且不会使用下载的资源更新缓存。</li> + <li><code>reload</code> — 浏览器直接从远程服务器获取资源,不查看缓存,然后使用下载的资源更新缓存。</li> + <li><code>no-cache</code> — 浏览器在其HTTP缓存中寻找匹配的请求。 + <ul> + <li>如果有匹配,无论是新的还是陈旧的,浏览器都会向远程服务器发出条件请求。如果服务器指示资源没有更改,则将从缓存中返回该资源。否则,将从服务器下载资源并更新缓存。</li> + <li>如果没有匹配,浏览器将发出正常请求,并使用下载的资源更新缓存。</li> + </ul> + <code>force-cache </code>— 浏览器在其HTTP缓存中寻找匹配的请求。 + + <ul> + <li>如果有匹配项,不管是新匹配项还是旧匹配项,都将从缓存中返回。</li> + <li>如果没有匹配,浏览器将发出正常请求,并使用下载的资源更新缓存。</li> + </ul> + </li> + <li><code>only-if-cached</code> — 浏览器在其HTTP缓存中寻找匹配的请求。 + <ul> + <li>如果有匹配项(新的或旧的),则从缓存中返回。</li> + <li>如果没有匹配,浏览器将返回一个错误。</li> + </ul> + The <code>"only-if-cached"</code> mode can only be used if the request's <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/Request/mode">mode</a></code> is <code>"same-origin"</code>. Cached redirects will be followed if the request's <code>redirect</code> property is <code>"follow"</code> and the redirects do not violate the <code>"same-origin"</code> mode.</li> +</ul> + +<h2 id="Example">Example</h2> + +<pre class="brush: js">// Download a resource with cache busting, to bypass the cache +// completely. +fetch("some.json", {cache: "no-store"}) + .then(function(response) { /* consume the response */ }); + +// Download a resource with cache busting, but update the HTTP +// cache with the downloaded resource. +fetch("some.json", {cache: "reload"}) + .then(function(response) { /* consume the response */ }); + +// Download a resource with cache busting when dealing with a +// properly configured server that will send the correct ETag +// and Date headers and properly handle If-Modified-Since and +// If-None-Match request headers, therefore we can rely on the +// validation to guarantee a fresh response. +fetch("some.json", {cache: "no-cache"}) + .then(function(response) { /* consume the response */ }); + +// Download a resource with economics in mind! Prefer a cached +// albeit stale response to conserve as much bandwidth as possible. +fetch("some.json", {cache: "force-cache"}) + .then(function(response) { /* consume the response */ });</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-cache','cache')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.Request.cache")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/clone/index.html b/files/zh-cn/web/api/request/clone/index.html new file mode 100644 index 0000000000..a256435381 --- /dev/null +++ b/files/zh-cn/web/api/request/clone/index.html @@ -0,0 +1,62 @@ +--- +title: Request.clone() +slug: Web/API/Request/clone +translation_of: Web/API/Request/clone +--- +<div>{{APIRef("Fetch")}}</div> + +<div></div> + +<p> {{domxref("Request")}} 接口中的<strong><code>clone()</code></strong> 方法可以创建一个当前<code>Request</code> 对象的副本。</p> + +<p> 如果响应体{{domxref("Body")}}已经被使用过,那么<code>clone()会抛出一个</code>{{jsxref("TypeError")}}。实际上,<code>clone()</code> 的主要作用就是支持{{domxref("Body")}}对象的多次使用</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var newRequest = request.clone();</pre> + +<h3 id="参数">参数</h3> + +<p>无。</p> + +<h3 id="返回值">返回值</h3> + +<p>{{domxref("Request")}}对象,也就是<code>Request的完整拷贝</code></p> + +<h2 id="示例">示例</h2> + +<p>在下面的代码中,我们使用<code>{{domxref("Request.Request()")}}构造函数创建了一个新的request对象(请求当前文件夹中的一个图片文件),然后拷贝了这个request对象。</code></p> + +<pre class="brush: js">var myRequest = new Request('flowers.jpg'); +var newRequest = myRequest.clone(); // a copy of the request is now stored in newRequest</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">说明</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-clone','clone')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>初始化定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Request.clone")}}</p> + +<h2 id="了解更多">了解更多</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/context/index.html b/files/zh-cn/web/api/request/context/index.html new file mode 100644 index 0000000000..c1ecbc28a0 --- /dev/null +++ b/files/zh-cn/web/api/request/context/index.html @@ -0,0 +1,43 @@ +--- +title: Request.context +slug: Web/API/Request/context +translation_of: Web/API/Request/context +--- +<div>{{APIRef("Fetch")}}{{deprecated_header()}}</div> + +<p><span class="seoSummary">The deprecated </span><font><font>弃用</font></font><strong><code>context</code></strong><font><font>所述的只读属性{{domxref(“请求”)}}接口包含请求的上下文(例如,</font></font><code>audio</code><font><font>,</font></font><code>image</code><font><font>,</font></font><code>iframe</code><font><font>)。</font></font><font><font>这定义了要获取的资源类型。</font><font>它已由{{domxref(“ Request.destination”,“ destination”)}}属性取代。</font></font> This defines what sort of resource is being fetched. This has been replaced by the {{domxref("Request.destination", "destination")}} property.</p> + +<p>The context of a request is only relevant in the <font><font>请求的上下文仅与</font></font><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API"><font><font>ServiceWorker API</font></font></a><font><font>相关</font><font>;</font><font>服务人员可以根据URL是用于图像还是可嵌入对象(例如{{htmlelement(“视频”)}},{{domxref(“ iframe”)}}等)进行决策。</font></font>; a service worker can make decisions based on whether the URL is for an image, or an embeddable object such as a {{htmlelement("video")}}, {{domxref("iframe")}}, etc.</p> + +<div class="note"> +<p><strong>Note<font><font>注意</font></font></strong><font><font>:您可以在“ </font></font><a href="https://fetch.spec.whatwg.org/#concept-request-context"><font><font>获取规范请求上下文”</font></font></a><font><font>部分中</font><font>找到不同可用上下文的完整列表,包括关联的上下文框架类型,CSP指令和平台功能示例</font><font>。</font></font> section.</p> +</div> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate">var <var>myContext</var> = <var>request</var>.context;</pre> + +<h3 id="Value">Value</h3> + +<p>A {{domxref("RequestContext")}} value.一个{{domxref(“ RequestContext”)}}值。</p> + +<h2 id="Example例子">Example<font><font>例子</font></font></h2> + +<p>In the following snippet, we create a new request using the {{domxref("Request.Request()")}} constructor (for an image file in the same directory as the script), then save the request context in a variable:在以下代码段中,我们使用{{domxref(“ Request.Request()”)}}}构造函数创建一个新请求(用于与脚本位于同一目录中的图像文件),然后将请求上下文保存在变量中:</p> + +<pre class="brush: js notranslate">var myRequest = new Request('flowers.jpg'); +var myContext = myRequest.context; // returns the empty string by default</pre> + +<h2 id="Browser_compatibility浏览器兼容性">Browser compatibility<font><font>浏览器兼容性</font></font></h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <font><font>此页面上的兼容性表是根据结构化数据生成的。</font><font>如果您想提供数据,请查看</font></font><a href="https://github.com/mdn/browser-compat-data"><font><font>https://github.com/mdn/browser-compat-data</font></font></a><font><font>并向我们发送请求请求。</font></font> and send us a pull request.</div> + +<p>{{Compat("api.Request.context")}}</p> + +<h2 id="更多">更多</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/credentials/index.html b/files/zh-cn/web/api/request/credentials/index.html new file mode 100644 index 0000000000..f7a57ad6c0 --- /dev/null +++ b/files/zh-cn/web/api/request/credentials/index.html @@ -0,0 +1,64 @@ +--- +title: Request.credentials +slug: Web/API/Request/credentials +tags: + - API + - Fetch + - 属性 + - 证书 + - 请求 +translation_of: Web/API/Request/credentials +--- +<div>{{APIRef("Fetch")}}</div> + +<p><strong><code>credentials</code></strong> 是{{domxref("Request")}}接口的只读属性,用于表示用户代理是否应该在跨域请求的情况下从其他域发送cookies。这与XHR的withCredentials 标志相似,不同的是有三个可选值(后者是两个):</p> + +<ul> + <li><code>omit</code>: 从不发送cookies.</li> + <li><code>same-origin</code>: 只有当URL与响应脚本同源才发送 cookies、 HTTP Basic authentication 等验证信息.(浏览器默认值,在旧版本浏览器,例如safari 11依旧是omit,safari 12已更改)</li> + <li><code>include</code>: 不论是不是跨域的请求,总是发送请求资源域在本地的 cookies、 HTTP Basic authentication 等验证信息.</li> +</ul> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var myCred = request.credentials;</pre> + +<h3 id="Value">Value</h3> + +<p>A {{domxref("RequestCredentials")}} value.</p> + +<h2 id="举例">举例</h2> + +<p>在以下代码中,我们使用{{domxref("Request.Request()")}}创建了一个新的request(为了一个与脚本在同一目录下的图片文件), 接着将request credentials存入一个变量:</p> + +<pre class="brush: js">var myRequest = new Request('flowers.jpg'); +var myCred = myRequest.credentials; // returns "same-origin" by default</pre> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-credentials','credentials')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.Request.credentials")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/headers/index.html b/files/zh-cn/web/api/request/headers/index.html new file mode 100644 index 0000000000..37e7f41654 --- /dev/null +++ b/files/zh-cn/web/api/request/headers/index.html @@ -0,0 +1,151 @@ +--- +title: Request.headers +slug: Web/API/Request/headers +translation_of: Web/API/Request/headers +--- +<div>{{APIRef("Fetch")}}</div> + +<p>{{domxref("Request")}}接口的只读属性 <strong><code>headers</code></strong> 包含与当前请求关联的{{domxref("Headers")}}对象.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <var>myHeaders</var> = <var>request</var>.headers;</pre> + +<h3 id="值">值</h3> + +<p>一个 {{domxref("Headers")}} 对象.</p> + +<h2 id="例子">例子</h2> + +<p>在下面的代码段中,我们使用 {{domxref("Request.Request()")}} 构造函数(为获取与脚本处于同一目录的图片文件)创建新请求,然后将请求headers保存到变量中:</p> + +<pre class="brush: js">var myRequest = new Request('flowers.jpg'); +var myHeaders = myRequest.headers; // Headers {}</pre> + +<p>使用 {{domxref("Headers.append")}} 向 {{domxref("Headers")}} 对象中添加header;然后,使用第二个init参数创建一个新的 <code>Request</code> ,同时,传递headers作为一个init选项:</p> + +<pre class="brush: js">var myHeaders = new Headers(); +myHeaders.append('Content-Type', 'image/jpeg'); + +var myInit = { method: 'GET', + headers: myHeaders, + mode: 'cors', + cache: 'default' }; + +var myRequest = new Request('flowers.jpg',myInit); + +myContentType = myRequest.headers.get('Content-Type'); // returns 'image/jpeg'</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-headers','headers')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Request.headers")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> + +<div id="SL_balloon_obj" style="display: block;"> +<div class="SL_ImTranslatorLogo" id="SL_button" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%; display: none; opacity: 1; left: -8px; top: -25px;"> </div> + +<div id="SL_shadow_translation_result2" style="display: none;"> </div> + +<div id="SL_shadow_translator" style="display: none;"> +<div id="SL_planshet"> +<div id="SL_arrow_up" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div> + +<div id="SL_Bproviders"> +<div class="SL_BL_LABLE_ON" id="SL_P0" title="Google">G</div> + +<div class="SL_BL_LABLE_ON" id="SL_P1" title="Microsoft">M</div> + +<div class="SL_BL_LABLE_ON" id="SL_P2" title="Translator">T</div> +</div> + +<div id="SL_alert_bbl" style="display: none;"> +<div id="SLHKclose" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div> + +<div id="SL_alert_cont"> </div> +</div> + +<div id="SL_TB"> +<table id="SL_tables"> + <tbody> + <tr> + <td class="SL_td"><input></td> + <td class="SL_td"><select><option value="auto">Detect language</option><option value="af">Afrikaans</option><option value="sq">Albanian</option><option value="ar">Arabic</option><option value="hy">Armenian</option><option value="az">Azerbaijani</option><option value="eu">Basque</option><option value="be">Belarusian</option><option value="bn">Bengali</option><option value="bs">Bosnian</option><option value="bg">Bulgarian</option><option value="ca">Catalan</option><option value="ceb">Cebuano</option><option value="ny">Chichewa</option><option value="zh-CN">Chinese (Simplified)</option><option value="zh-TW">Chinese (Traditional)</option><option value="hr">Croatian</option><option value="cs">Czech</option><option value="da">Danish</option><option value="nl">Dutch</option><option value="en">English</option><option value="eo">Esperanto</option><option value="et">Estonian</option><option value="tl">Filipino</option><option value="fi">Finnish</option><option value="fr">French</option><option value="gl">Galician</option><option value="ka">Georgian</option><option value="de">German</option><option value="el">Greek</option><option value="gu">Gujarati</option><option value="ht">Haitian Creole</option><option value="ha">Hausa</option><option value="iw">Hebrew</option><option value="hi">Hindi</option><option value="hmn">Hmong</option><option value="hu">Hungarian</option><option value="is">Icelandic</option><option value="ig">Igbo</option><option value="id">Indonesian</option><option value="ga">Irish</option><option value="it">Italian</option><option value="ja">Japanese</option><option value="jw">Javanese</option><option value="kn">Kannada</option><option value="kk">Kazakh</option><option value="km">Khmer</option><option value="ko">Korean</option><option value="lo">Lao</option><option value="la">Latin</option><option value="lv">Latvian</option><option value="lt">Lithuanian</option><option value="mk">Macedonian</option><option value="mg">Malagasy</option><option value="ms">Malay</option><option value="ml">Malayalam</option><option value="mt">Maltese</option><option value="mi">Maori</option><option value="mr">Marathi</option><option value="mn">Mongolian</option><option value="my">Myanmar (Burmese)</option><option value="ne">Nepali</option><option value="no">Norwegian</option><option value="fa">Persian</option><option value="pl">Polish</option><option value="pt">Portuguese</option><option value="pa">Punjabi</option><option value="ro">Romanian</option><option value="ru">Russian</option><option value="sr">Serbian</option><option value="st">Sesotho</option><option value="si">Sinhala</option><option value="sk">Slovak</option><option value="sl">Slovenian</option><option value="so">Somali</option><option value="es">Spanish</option><option value="su">Sundanese</option><option value="sw">Swahili</option><option value="sv">Swedish</option><option value="tg">Tajik</option><option value="ta">Tamil</option><option value="te">Telugu</option><option value="th">Thai</option><option value="tr">Turkish</option><option value="uk">Ukrainian</option><option value="ur">Urdu</option><option value="uz">Uzbek</option><option value="vi">Vietnamese</option><option value="cy">Welsh</option><option value="yi">Yiddish</option><option value="yo">Yoruba</option><option value="zu">Zulu</option></select></td> + <td class="SL_td"> + <div id="SL_switch_b" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Switch languages"> </div> + </td> + <td class="SL_td"><select><option value="af">Afrikaans</option><option value="sq">Albanian</option><option value="ar">Arabic</option><option value="hy">Armenian</option><option value="az">Azerbaijani</option><option value="eu">Basque</option><option value="be">Belarusian</option><option value="bn">Bengali</option><option value="bs">Bosnian</option><option value="bg">Bulgarian</option><option value="ca">Catalan</option><option value="ceb">Cebuano</option><option value="ny">Chichewa</option><option value="zh-CN">Chinese (Simplified)</option><option value="zh-TW">Chinese (Traditional)</option><option value="hr">Croatian</option><option value="cs">Czech</option><option value="da">Danish</option><option value="nl">Dutch</option><option selected value="en">English</option><option value="eo">Esperanto</option><option value="et">Estonian</option><option value="tl">Filipino</option><option value="fi">Finnish</option><option value="fr">French</option><option value="gl">Galician</option><option value="ka">Georgian</option><option value="de">German</option><option value="el">Greek</option><option value="gu">Gujarati</option><option value="ht">Haitian Creole</option><option value="ha">Hausa</option><option value="iw">Hebrew</option><option value="hi">Hindi</option><option value="hmn">Hmong</option><option value="hu">Hungarian</option><option value="is">Icelandic</option><option value="ig">Igbo</option><option value="id">Indonesian</option><option value="ga">Irish</option><option value="it">Italian</option><option value="ja">Japanese</option><option value="jw">Javanese</option><option value="kn">Kannada</option><option value="kk">Kazakh</option><option value="km">Khmer</option><option value="ko">Korean</option><option value="lo">Lao</option><option value="la">Latin</option><option value="lv">Latvian</option><option value="lt">Lithuanian</option><option value="mk">Macedonian</option><option value="mg">Malagasy</option><option value="ms">Malay</option><option value="ml">Malayalam</option><option value="mt">Maltese</option><option value="mi">Maori</option><option value="mr">Marathi</option><option value="mn">Mongolian</option><option value="my">Myanmar (Burmese)</option><option value="ne">Nepali</option><option value="no">Norwegian</option><option value="fa">Persian</option><option value="pl">Polish</option><option value="pt">Portuguese</option><option value="pa">Punjabi</option><option value="ro">Romanian</option><option value="ru">Russian</option><option value="sr">Serbian</option><option value="st">Sesotho</option><option value="si">Sinhala</option><option value="sk">Slovak</option><option value="sl">Slovenian</option><option value="so">Somali</option><option value="es">Spanish</option><option value="su">Sundanese</option><option value="sw">Swahili</option><option value="sv">Swedish</option><option value="tg">Tajik</option><option value="ta">Tamil</option><option value="te">Telugu</option><option value="th">Thai</option><option value="tr">Turkish</option><option value="uk">Ukrainian</option><option value="ur">Urdu</option><option value="uz">Uzbek</option><option value="vi">Vietnamese</option><option value="cy">Welsh</option><option value="yi">Yiddish</option><option value="yo">Yoruba</option><option value="zu">Zulu</option></select></td> + <td class="SL_td"> + <div id="SL_TTS_voice" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Listen to the translation"> </div> + </td> + <td class="SL_td"> + <div class="SL_copy" id="SL_copy" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Copy translation"> </div> + </td> + <td class="SL_td"> + <div id="SL_bbl_font_patch"> </div> + + <div class="SL_bbl_font" id="SL_bbl_font" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Font size"> </div> + </td> + <td class="SL_td"> + <div id="SL_bbl_help" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Help"> </div> + </td> + <td class="SL_td"> + <div class="SL_pin_off" id="SL_pin" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Pin pop-up bubble"> </div> + </td> + </tr> + </tbody> +</table> +</div> +</div> + +<div id="SL_shadow_translation_result" style=""> </div> + +<div class="SL_loading" id="SL_loading" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div> + +<div id="SL_player2"> </div> + +<div id="SL_alert100">Text-to-speech function is limited to 200 characters</div> + +<div id="SL_Balloon_options" style="background: rgb(255, 255, 255) repeat scroll 0% 0%;"> +<div id="SL_arrow_down" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div> + +<table id="SL_tbl_opt" style="width: 100%;"> + <tbody> + <tr> + <td><input></td> + <td> + <div id="SL_BBL_IMG" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Show Translator's button 3 second(s)"> </div> + </td> + <td><a class="SL_options" title="Show options">Options</a> : <a class="SL_options" title="Translation History">History</a> : <a class="SL_options" title="ImTranslator Feedback">Feedback</a> : <a class="SL_options" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GD9D8CPW8HFA2" title="Make a small contribution">Donate</a></td> + <td><span id="SL_Balloon_Close" title="Close">Close</span></td> + </tr> + </tbody> +</table> +</div> +</div> +</div> diff --git a/files/zh-cn/web/api/request/index.html b/files/zh-cn/web/api/request/index.html new file mode 100644 index 0000000000..10248d0e2f --- /dev/null +++ b/files/zh-cn/web/api/request/index.html @@ -0,0 +1,170 @@ +--- +title: Request +slug: Web/API/Request +translation_of: Web/API/Request +--- +<div>{{APIRef("Fetch")}}{{SeeCompatTable}}</div> + +<div><a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a> 的 <strong><code>Request</code></strong>接口?用来表示资源请求。</div> + +<div></div> + +<p>你可以使用 {{domxref("Request.Request()")}} ?构造函数创建一个<code>Request 对象,但是你可能会遇到一个 Request 对象作为其它 API 的操作被返回,比如一个 </code>service worker的{{domxref("FetchEvent.request")}}。</p> + +<h2 id="构造器">构造器</h2> + +<dl> + <dt>{{domxref("Request.Request()")}}</dt> + <dd>创建一个新的 <code>Request</code> 对象。</dd> +</dl> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("Request.method")}} {{readonlyInline}}</dt> + <dd>包含请求的方法 (<code>GET</code>, <code>POST</code>, 等.)</dd> + <dt>{{domxref("Request.url")}} {{readonlyInline}}</dt> + <dd>包含这个请求的URL。</dd> + <dt>{{domxref("Request.headers")}} {{readonlyInline}}</dt> + <dd>包含请求相关的{{domxref("Headers")}}对象。</dd> + <dt>{{domxref("Request.context")}} {{readonlyInline}} {{deprecated_inline()}}</dt> + <dd>包含请求的上下文(例如:<code>audio</code>, <code>image</code>, <code>iframe</code>, 等)</dd> + <dt>{{domxref("Request.referrer")}} {{readonlyInline}}</dt> + <dd>?包含请求的来源 (例如:<code>client</code>)。</dd> + <dt>{{domxref("Request.referrerPolicy")}} {{readonlyInline}}</dt> + <dd>?包含请求来源的策略 (例如:<code>no-referrer</code>)。</dd> + <dt>{{domxref("Request.mode")}} {{readonlyInline}}</dt> + <dd>包含请求的模式 (例如: <code>cors</code>, <code>no-cors</code>, <code>same-origin</code>,<code> navigate</code>).</dd> + <dt>{{domxref("Request.credentials")}} {{readonlyInline}}</dt> + <dd>包含请求的证书(例如: <code>omit</code>, <code>same-origin</code>).</dd> + <dt>{{domxref("Request.redirect")}} {{readonlyinline}}</dt> + <dd>包含?如何处理重定向模式,它可能是一个<code> follow </code>,<code>error</code>或者<code>manual</code>。</dd> + <dt>{{domxref("Request.integrity")}} {{readonlyInline}}</dt> + <dd>包含请求的<a href="/en-US/docs/Web/Security/Subresource_Integrity">子资源的完整性</a>值 (例如: <code>sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=</code>).</dd> + <dt>{{domxref("Request.cache")}} {{readonlyInline}}</dt> + <dd>包含请求的缓存模式 (例如: <code>default</code>, <code>reload</code>, <code>no-cache</code>).</dd> +</dl> + +<p><code>Request</code>实现了{{domxref("Body")}}, 所以它还具有以下属性可用:</p> + +<dl> + <dt>{{domxref("Body.body")}} {{readonlyInline}}</dt> + <dd>一个简单getter用于曝光一个{{domxref("ReadableStream")}}的主体内容.</dd> +</dl> + +<dl> + <dt>{{domxref("Body.bodyUsed")}} {{readonlyInline}}</dt> + <dd>存储一个{{domxref("Boolean")}}判断主体是否已经被用于一个响应中.</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("Request.clone()")}}</dt> + <dd>创建当前request的副本。</dd> +</dl> + +<p><code>Request</code>实现 {{domxref("Body")}}, 因此它也有以下方法可用:</p> + +<dl> + <dt>{{domxref("Body.arrayBuffer()")}}</dt> + <dd>返回解决一个{{domxref("ArrayBuffer")}}表示的请求主体的promise.</dd> + <dt>{{domxref("Body.blob()")}}</dt> + <dd>返回解决一个{{domxref("Blob")}}表示的请求主体的promise.</dd> + <dt>{{domxref("Body.formData()")}}</dt> + <dd>返回解决一个{{domxref("FormData")}}表示的请求主体的promise.</dd> + <dt>{{domxref("Body.json()")}}</dt> + <dd>返回解决一个{{domxref("JSON")}}表示的请求主体的promise.</dd> + <dt>{{domxref("Body.text()")}}</dt> + <dd>返回解决一个{{domxref("USVString")}}(文本)表示的请求主体的promise.</dd> +</dl> + +<p class="note">注意:这些Body功能只能运行一次; 随后的调用将通过空strings/ ArrayBuffers解析.</p> + +<h2 id="示例">示例</h2> + +<p>在下面的代码中,我们使用 Request ( ) 构造函数创建了一个新的 request实例 (用来请求同一目录下的图片), 然后返回请求的一些属性。</p> + +<pre class="brush: js">const myRequest = new Request('http://localhost/flowers.jpg'); + +const myURL = myRequest.url; // http://localhost/flowers.jpg +const myMethod = myRequest.method; // GET +const myCred = myRequest.credentials; // omit +</pre> + +<p>然后,通过将Request对象作为参数传递给<a href="/en-US/docs/Web/API/GlobalFetch/fetch">{{domxref("GlobalFetch.fetch()")}}</a>调用来获取此请求,例如:</p> + +<pre class="brush: js line-numbers language-js">fetch(myRequest) + .then(response => response.blob()) + .then(blob => { + myImage.src = URL.createObjectURL(blob); + }); + +</pre> + +<p>在下面的代码片段中,我们使用<code>Request()</code>构造函数创建了一个新的request,其中包含一些初始数据和正文内容,用于需要主体有效载荷的api请求:</p> + +<pre class="brush: js line-numbers language-js">const myRequest = new Request('http://localhost/api', {method: 'POST', body: '{"foo":"bar"}'}); + +const myURL = myRequest.url; // http://localhost/api +const myMethod = myRequest.method; // POST +const myCred = myRequest.credentials; // omit +const bodyUsed = myRequest.bodyUsed; +</pre> + +<p class="note">注意:body类型只能是一个{{domxref("Blob")}},{{domxref("BufferSource")}}, {{domxref("FormData")}}, {{domxref("URLSearchParams")}}, {{domxref("USVString")}} 或者{{domxref("ReadableStream")}}类型,因此增加一个JSON对象的有效载荷则需要字符串化该对象.</p> + +<p>例如,您可以通过将<code>Request</code>对象作为参数传递给{{domxref("GlobalFetch.fetch()")}}调用来获取此api请求,并获得响应:</p> + +<pre>fetch(myRequest) + .then(response => { + if (response.status === 200) { + return response.json(); + } else { + throw new Error('Something went wrong on api server!'); + } + }) + .then(response => { + console.debug(response); + // ... + }).catch(error => { + console.error(error); + }); +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#request-class','Request')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<div id="compat-mobile"> + + + + +<p>{{Compat("api.Request")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/method/index.html b/files/zh-cn/web/api/request/method/index.html new file mode 100644 index 0000000000..a10396fe27 --- /dev/null +++ b/files/zh-cn/web/api/request/method/index.html @@ -0,0 +1,113 @@ +--- +title: Request.method +slug: Web/API/Request/method +translation_of: Web/API/Request/method +--- +<div>{{APIRef("Fetch")}}{{SeeCompatTable}}</div> + +<p>{{domxref("Request")}}的只读属性<strong>method</strong>包含请求的方法(<code>GET</code>, <code>POST</code>, etc.)</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <var>myMethod</var> = <var>request</var>.method;</pre> + +<h3 id="Value">Value</h3> + +<p>A {{domxref("ByteString")}} indicating the method of the request.</p> + +<h2 id="举例">举例</h2> + +<p>In the following snippet, we create a new request using the {{domxref("Request.Request()")}} constructor (for an image file in the same directory as the script), then save the method of the request in a variable:</p> + +<pre class="brush: js">var myRequest = new Request('flowers.jpg'); +var myMethod = myRequest.method; // GET</pre> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-method','method')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(42)}}<br> + {{CompatChrome(41)}}<sup>[1]</sup></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(39)}}<br> + 34<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td> + <p>29<br> + 28<sup>[1]</sup></p> + </td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] This feature is implemented behind a preference.</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/mode/index.html b/files/zh-cn/web/api/request/mode/index.html new file mode 100644 index 0000000000..ae3bb32f8a --- /dev/null +++ b/files/zh-cn/web/api/request/mode/index.html @@ -0,0 +1,80 @@ +--- +title: Request.mode +slug: Web/API/Request/mode +tags: + - API + - Fetch + - 参考 + - 属性 + - 请求 +translation_of: Web/API/Request/mode +--- +<div>{{APIRef("Fetch")}}</div> + +<p>{{domxref("Request")}} 接口的 <strong><code>mode</code></strong> 只读属性包含请求的模式(例如:<code>cors</code> 、 <code>no-cors</code> 、 <code>cors-with-forced-preflight</code> 、 <code>same-origin</code> 或 <code>navigate</code> 。)这用于确定跨域请求是否能得到有效的响应,以及响应的哪些属性是可读的。</p> + +<ul> +</ul> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var <var>myMode</var> = <var>request</var>.mode;</pre> + +<h3 id="属性值">属性值</h3> + +<p>一个 <code>RequestMode</code> 值。</p> + +<ul> + <li><code>same-origin</code> — 如果使用此模式向另外一个源发送请求,显而易见,结果会是一个错误。你可以设置该模式以确保请求总是向当前的源发起的。</li> + <li><code>no-cors</code> — 保证请求对应的 method 只有 <code>HEAD</code>,<code>GET</code> 或 <code>POST</code> 方法,并且请求的 headers 只能有简单请求头 (<a href="https://fetch.spec.whatwg.org/#simple-header">simple headers</a>)。如果 ServiceWorker 劫持了此类请求,除了 <a href="https://fetch.spec.whatwg.org/#simple-header">simple header</a> 之外,不能添加或修改其他 header。另外 JavaScript 不会读取 {{domxref("Response")}} 的任何属性。这样将会确保 ServiceWorker 不会影响 Web 语义(semantics of the Web),同时保证了在跨域时不会发生安全和隐私泄露的问题。</li> + <li><code>cors</code> — 允许跨域请求,例如访问第三方供应商提供的各种 API。预期将会遵守 <a href="/zh-CN/docs/Web/HTTP/Access_control_CORS">CORS protocol</a> 。仅有<a href="https://fetch.spec.whatwg.org/#concept-filtered-response-cors">有限部分</a>的头部暴露在 {{domxref("Response")}} ,但是 body 部分是可读的。</li> + <li><code>navigate</code> — 表示这是一个浏览器的页面切换请求(request)。 navigate请求仅在浏览器切换页面时创建,该请求应该返回HTML。</li> +</ul> + +<h4 id="默认模式">默认模式</h4> + +<p>可以以多种方式发起请求,并且请求的模式取决于发起请求的特定方式。</p> + +<p>例如,当一个 <code>Request</code> 对象以 {{domxref("Request.Request")}} 方式创建,该<code>Request</code> 的 <code>mode</code> 的值为 <code>cors</code> 。</p> + +<p>然而,除了以 {{domxref("Request.Request")}} 创建的请求,模式通常为 <code>no-cors</code> 。例如,对与嵌入资源发起的请求,除非存在 <code><a href="/zh-CN/docs/Web/HTML/CORS_settings_attributes">crossorigin</a></code> 属性,即对于<span> {{HTMLElement("link")}} 、 {{HTMLElement("script")}} (除了和模块一起使用之外)、 {{HTMLElement("img")}}、 {{HTMLElement("audio")}}、 {{HTMLElement("video")}}、 {{HTMLElement("object")}}、 {{HTMLElement("embed")}}还有 {{HTMLElement("iframe")}} 元素,</span>在大多数情况下是使用 <code>no-cors</code> 模式<span>。</span></p> + +<h2 id="示例">示例</h2> + +<p>在下面代码段中,我们使用 {{domxref("Request.Request()")}} 创建请求(请求与脚本位于同一目录中的图像文件),然后将请求模式保存在一个变量中:</p> + +<p>In the following snippet, we create a new request using theconstructor (for an image file in the same directory as the script), then save the request mode in a variable:</p> + +<pre class="brush: js notranslate">var myRequest = new Request('flowers.jpg'); +var myMode = myRequest.mode; // returns "cors" by default</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-mode', 'mode')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Request.mode")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/zh-CN/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/zh-CN/docs/Web/HTTP/Access_control_CORS">HTTP 访问控制(CORS)</a></li> + <li><a href="/zh-CN/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/request/index.html b/files/zh-cn/web/api/request/request/index.html new file mode 100644 index 0000000000..369f8d4e7f --- /dev/null +++ b/files/zh-cn/web/api/request/request/index.html @@ -0,0 +1,158 @@ +--- +title: Request() +slug: Web/API/Request/Request +tags: + - API + - Fetch + - Reference + - request + - 实验性功能 + - 构造函数 +translation_of: Web/API/Request/Request +--- +<p>{{APIRef("Fetch")}}{{ SeeCompatTable() }}</p> + +<p><code><strong>Request()</strong></code> 构造器创建一个新的{{domxref("Request")}} 对象。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var myRequest = new Request(input[, init]);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><em>input</em></dt> + <dd>定义你想要fetch的资源。可以是下面两者之一: + <ul> + <li>一个直接包含你希望 fetch 的资源的 URL 的 {{domxref("USVString")}}。</li> + <li>一个 {{domxref("Request")}} 对象。请注意以下行为更新,以在保留安全性的同时使构造函数不太可能引发异常: + <ul> + <li>如果此对象存在于构造函数调用的另一个起源上,则将除去{{domxref("Request.referrer")}}。</li> + <li>如果此对象的导航为 {{domxref("Request.mode")}},则<code>mode</code>将转换为<code>same-origin</code>。</li> + </ul> + </li> + </ul> + </dd> + <dt><em>init</em> {{optional_inline}}</dt> + <dd>一个可选对象,包含希望被包括到请求中的各种自定义选项。可用的选项如下: + <ul> + <li><code>method</code>: 请求的方法,例如:<code>GET</code>, <code>POST。</code></li> + <li><code>headers</code>: 任何你想加到请求中的头,其被放在{{domxref("Headers")}}对象或内部值为{{domxref("ByteString")}} 的对象字面量中。</li> + <li><code>body</code>: 任何你想加到请求中的body,可以是{{domxref("Blob")}}, {{domxref("BufferSource")}}, {{domxref("FormData")}}, {{domxref("URLSearchParams")}}, 或 {{domxref("USVString")}}对象。注意<code>GET</code> 和 <code>HEAD请求没有body。</code></li> + <li><code>mode</code>: 请求的模式, 比如 <code>cors</code>, <code>no-cors</code>, <code>same-origin</code>, 或 <code>navigate</code>。默认值为 <code>cors</code>。</li> + <li><code>credentials</code>: 想要在请求中使用的credentials:: <code>omit</code>, <code>same-origin</code>, 或 <code>include</code>。默认值应该为<code>omit</code>。但在Chrome中,Chrome 47 之前的版本默认值为 <code>same-origin</code> ,自Chrome 47起,默认值为<code>include。</code></li> + <li><code>cache</code>: 请求中想要使用的 <a href="/en-US/docs/Web/API/Request/cache">cache mode</a> </li> + <li><code>redirect</code>: 对重定向处理的模式: <code>follow</code>, <code>error</code>, or <code>manual</code>。在Chrome中,Chrome 47 之前的版本默认值为 <code>manual</code> ,自Chrome 47起,默认值为<code>follow。</code></li> + <li><code>referrer</code>: 一个指定了<code>no-referrer</code>, <code>client</code>, 或一个 URL的 {{domxref("USVString")}} 。默认值是<code>about:client</code>。</li> + <li><code>integrity</code>: 包括请求的 <a href="/en-US/docs/Web/Security/Subresource_Integrity">subresource integrity</a> 值 (e.g., <code>sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=</code>).</li> + </ul> + </dd> +</dl> + +<h2 id="Errors">Errors</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col"><strong>Type</strong></th> + <th scope="col"><strong>Description</strong></th> + </tr> + </thead> + <tbody> + <tr> + <td>TypeError</td> + <td>自 <a href="/en-US/docs/Mozilla/Firefox/Releases/43">Firefox 43</a>后, 若URL有credentials,<code>Request()</code> 会抛出TypeError , 例如http://user:password@example.com。</td> + </tr> + </tbody> +</table> + +<h2 id="Example">Example</h2> + +<p>在我们的获取请求示例 <a href="https://github.com/mdn/fetch-examples/tree/gh-pages/fetch-request">Fetch Request example</a> (see <a href="http://mdn.github.io/fetch-examples/fetch-request/">Fetch Request live</a>) 中,我们使用构造函数创建一个新的<code>Request</code>对象, 然后使用 {{domxref("GlobalFetch.fetch")}} 发送请求. 由于我们正在获取图像, 我们在响应上运行 {{domxref("Body.blob")}} 以为其提供正确的 MIME 类型,以便对其进行正确处理, 然后为其创建一个 Object URL,并将其显示在 {{htmlelement("img")}} 元素中。</p> + +<pre class="brush: js notranslate">var myImage = document.querySelector('img'); + +var myRequest = new Request('flowers.jpg'); + +fetch(myRequest).then(function(response) { + return response.blob(); +}).then(function(response) { + var objectURL = URL.createObjectURL(response); + myImage.src = objectURL; +});</pre> + +<p>在<a href="https://github.com/mdn/fetch-examples/tree/gh-pages/fetch-request-with-init">Fetch Request with init example</a> (参见 <a href="http://mdn.github.io/fetch-examples/fetch-request-with-init/">Fetch Request init live</a>) 我们做了同样的事情,只不过我们在调用<code>fetch()时,还</code>传递进了一个 init 对象:</p> + +<pre class="brush: js notranslate">var myImage = document.querySelector('img'); + +var myHeaders = new Headers(); +myHeaders.append('Content-Type', 'image/jpeg'); + +var myInit = { method: 'GET', + headers: myHeaders, + mode: 'cors', + cache: 'default' }; + +var myRequest = new Request('flowers.jpg',myInit); + +fetch(myRequest).then(function(response) { + ... +});</pre> + +<p>注意你也可以把 init 对象作为参数传递到<code>fetch</code>调用中来达到同样的效果。如下:</p> + +<pre class="brush: js notranslate">fetch(myRequest,myInit).then(function(response) { + ... +});</pre> + +<p>也可以使用在init中使用对象字面量作为<code>headers。</code></p> + +<pre class="brush: js notranslate">var myInit = { method: 'GET', + headers: { + 'Content-Type': 'image/jpeg' + }, + mode: 'cors', + cache: 'default' }; + +var myRequest = new Request('flowers.jpg', myInit); +</pre> + +<p>也可以把 {{domxref("Request")}} 对象再作参数传递进 <code>Request()</code> 构造器来创建一个请求的副本(就像调用{{domxref("Request.clone","clone()")}}一样)。</p> + +<div class="codehilite" style="background: #f0f3f3;"> +<pre style="line-height: 125%;">var copy = new Request(myRequest); +</pre> +</div> + +<div class="note"> +<p><strong>Note</strong>: This last usage is probably only useful in <a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorkers</a>.</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request','Request()')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.Request.Request")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> diff --git a/files/zh-cn/web/api/request/url/index.html b/files/zh-cn/web/api/request/url/index.html new file mode 100644 index 0000000000..de2a6d17f1 --- /dev/null +++ b/files/zh-cn/web/api/request/url/index.html @@ -0,0 +1,113 @@ +--- +title: Request.url +slug: Web/API/Request/url +translation_of: Web/API/Request/url +--- +<div>{{APIRef("Fetch")}}{{SeeCompatTable}}</div> + +<p>The <strong><code>url</code></strong> read-only property of the {{domxref("Request")}} interface contains the URL of the request.</p> + +<h2 id="句法">句法</h2> + +<pre class="syntaxbox">var <var>myURL</var> = <var>request</var>.url;</pre> + +<h3 id="Value">Value</h3> + +<p>A {{domxref("USVString")}} indicating the url of the request.</p> + +<h2 id="举例">举例</h2> + +<p>In the following snippet, we create a new request using the {{domxref("Request.Request()")}} constructor (for an image file in the same directory as the script), then save the url of the request in a variable:</p> + +<pre class="brush: js">var myRequest = new Request('flowers.jpg'); +var myURL = myRequest.url; // "http://mdn.github.io/fetch-examples/fetch-request/flowers.jpg"</pre> + +<h2 id="特性">特性</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Fetch','#dom-request-url','url')}}</td> + <td>{{Spec2('Fetch')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(42)}}<br> + {{CompatChrome(41)}}<sup>[1]</sup></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(39)}}<br> + 34<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td> + <p>29<br> + 28<sup>[1]</sup></p> + </td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] This feature is implemented behind a preference.</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> + <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> + <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> +</ul> |