XDomainRequest - Web APIs 编辑

Obsolete

This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

Non-standard

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

XDomainRequest is an implementation of HTTP access control (CORS) that worked in Internet Explorer 8 and 9. It was removed in Internet Explorer 10 in favor of using XMLHttpRequest with proper CORS; if you are targeting Internet Explorer 10 or later, or wish to support any other browser, you need to use standard HTTP access control.

This interface can send both GET and POST requests.

Syntax

var xdr = new XDomainRequest();

Returns a new XDomainRequest object, which can then be used to make and manage these requests.

Properties

XDomainRequest.timeout
Gets or sets the amount of time until a request times out.
XDomainRequest.responseText
Gets the response body as a string.

Methods

XDomainRequest.open()
Opens the request, specifying the method (GET/POST) and URL.
XDomainRequest.send()
Sends the request. POST data is specified in this method.
XDomainRequest.abort()
Aborts the request.

Event handlers

XDomainRequest.onprogress
A handler for when the request has made progress between the send method call and the onload event.
XDomainRequest.ontimeout
A handler for when the request times out.
XDomainRequest.onerror
A handler for when a request has errored.
XDomainRequest.onload
A handler for when the full response has been received from the server.

Example

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 Occurred
  };

  xdr.onload = function() {
    //success(xdr.responseText);
  }

  setTimeout(function () {
    xdr.send();
  }, 0);
}

Note: The xdr.send() call is wrapped in a timeout (see window.setTimeout() to prevent an issue with the interface where some requests are lost if multiple XDomainRequests are being sent at the same time.

Note: The xdr.onprogress event should always be defined, even as an empty function, or XDomainRequest may not fire onload for duplicate requests.

Security

The XDomainRequest is built to be secure in multiple ways.

  • The origin's security protocol must match that of the requested URL. (http to http, https to https). If these do not match, the request will error "Access is Denied".
  • The requested URL's server must have the Access-Control-Allow-Origin header set to either all ("*") or to include the origin of the request.

Specifications

This interface and its methods are non-standard.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:82 次

字数:5381

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文