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/xdomainrequest | |
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/xdomainrequest')
-rw-r--r-- | files/zh-cn/web/api/xdomainrequest/index.html | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/xdomainrequest/index.html b/files/zh-cn/web/api/xdomainrequest/index.html new file mode 100644 index 0000000000..dd7dce2899 --- /dev/null +++ b/files/zh-cn/web/api/xdomainrequest/index.html @@ -0,0 +1,186 @@ +--- +title: XDomainRequest +slug: Web/API/XDomainRequest +tags: + - AJAX + - API + - IE + - JavaScript + - Web + - 废弃 + - 微软 + - 非标准 +translation_of: Web/API/XDomainRequest +--- +<p>{{obsolete_header}}</p> + +<p>{{non-standard_header}}</p> + +<h2 id="摘要">摘要</h2> + +<p><span style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;">XDomainRequest是在IE8和IE9上的</span><a href="/en-US/docs/HTTP/Access_control_CORS" style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;">HTTP access control (CORS) </a>的实现,在IE10中被<span style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;"> 包含CORS的</span><a href="/en-US/docs/Web/API/XMLHttpRequest" style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;">XMLHttpRequest </a>取代了,如果你的开发目标是IE10或IE的后续版本,或想要支待其他的浏览器,你需要使用标准的<a href="/en-US/docs/Web/HTTP/Access_control_CORS" style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;">HTTP access control</a>。</p> + +<p>该接口可以发送GET和POST请求</p> + +<h2 id="语法">语法</h2> + +<pre>var xdr = new XDomainRequest();</pre> + +<p>返回<span style="font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 14.4444446563721px; line-height: 23.3333339691162px;">XDomainRequest的实例,该实例可以被用来生成或管理请求。</span></p> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("XDomainRequest.timeout")}}</dt> + <dd>获取或设置请求的过期时间。</dd> + <dt>{{domxref("XDomainRequest.responseText")}}</dt> + <dd>以字符串形式获取响应体。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("XDomainRequest.open()")}}</dt> + <dd>根据指定的方法(GET或POST)和URL,打开请求。</dd> + <dt>{{domxref("XDomainRequest.send()")}}</dt> + <dd>发送请求。POST的数据会在该方法中被指定。</dd> + <dt>{{domxref("XDomainRequest.abort()")}}</dt> + <dd>中止请求。</dd> +</dl> + +<h2 id="事件处理程序">事件处理程序</h2> + +<dl> + <dt>{{domxref("XDomainRequest.onprogress")}}</dt> + <dd>当请求中发送方法和onload事件中有进展时的处理程序。</dd> + <dt>{{domxref("XDomainRequest.ontimeout")}}</dt> + <dd>当请求超时时的事件处理程序。</dd> + <dt>{{domxref("XDomainRequest.onerror")}}</dt> + <dd>当请求发生错误时的处理程序。</dd> + <dt>{{domxref("XDomainRequest.onload")}}</dt> + <dd>当服务器端的响应被完整接收时的处理程序。</dd> +</dl> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">if(window.XDomainRequest){ + var xdr = new XDomainRequest(); + + xdr.open("get", "http://example.com/api/method"); + + xdr.onprogress = function () { + //Progress + }; + + xdr.ontimeout = function () { + //Timeout + }; + + xdr.onerror = function () { + //Error Occured + }; + + xdr.onload = function() { + //success(xdr.responseText); + } + + setTimeout(function () { + xdr.send(); + }, 0); +}</pre> + +<div> </div> + +<div class="note"> +<p><strong>注意: </strong>如果多个XDomainRequests同时被发送,一些请求可能会丢失,为避免这种情况,xdr.send()的调用应被包裹在setTimeout方法中(见{{domxref("window.setTimeout()")}})。</p> +</div> + +<h2 id="安全">安全</h2> + +<p><span style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;">XDomainRequest为了确保安全构建,采用了多种方法。</span></p> + +<ul> + <li>安全协议源必须匹配请求的URL。(http到http,https到https)。如果不匹配,请求会报“拒绝访问”的错误。</li> + <li>被请求的URL的服务器必须带有<span style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;"> 设置为(“*”)或包含了请求方的</span><code style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;"><a href="/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Origin" style="font-size: 14.4444446563721px; line-height: 23.3333339691162px;">Access-Control-Allow-Origin</a>的头部。</code></li> +</ul> + +<h2 id="标准">标准</h2> + +<p>该接口及其方法没有遵循标准。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>XDomainRequest</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>8.0-9.x</td> + <td>{{ CompatNo() }}</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>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>XDomainRequest</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + </tr> + </tbody> +</table> +</div> + +<section id="Quick_Links"> +<ol> + <li data-default-state="open"><a href="#"><strong>Properties</strong></a> + + <ol> + <li>{{domxref("XDomainRequest.timeout")}}</li> + <li>{{domxref("XDomainRequest.responseText")}}</li> + </ol> + </li> + <li data-default-state="open"><a href="#"><strong>Methods</strong></a> + <ol> + <li>{{domxref("XDomainRequest.open()")}}</li> + <li>{{domxref("XDomainRequest.send()")}}</li> + <li>{{domxref("XDomainRequest.abort()")}}</li> + </ol> + </li> + <li data-default-state="open"><a href="#"><strong>Event handlers</strong></a> + <ol> + <li>{{domxref("XDomainRequest.onprogress")}}</li> + <li>{{domxref("XDomainRequest.ontimeout")}}</li> + <li>{{domxref("XDomainRequest.onerror")}}</li> + <li>{{domxref("XDomainRequest.onload")}}</li> + </ol> + </li> +</ol> +</section> |