The XDomainRequest object has these types of members:

Events

The XDomainRequest object has these events.

Event Description
onerror

Raised when there is an error that prevents the completion of the cross-domain request.

onload

Raised when the object has been completely received from the server.

onprogress

Raised when the browser starts receiving data from the server.

ontimeout

Raised when there is an error that prevents the completion of the request.

Methods

The XDomainRequest object has these methods.

Method Description
abort

The abort method terminates a pending send.

abort

Cancels the current HTTP request.

open (XDomainRequest)

Creates a connection with a domain's server.

send (XDomainRequest)

Transmits a data string to the server for processing.

Properties

The XDomainRequest object has these properties.

Property Description

constructor

Returns a reference to the constructor of an object.

contentType

Gets the Content-Type property in the HTML request or response header.

responseText

Retrieves the response body as a string.

timeout

Gets or sets the time-out value.

Standards information

There are no standards that apply here.

Remarks

The XDomainRequest object is a safe, reliable, and lightweight data service that allows script on any document to anonymously connect to any server and exchange data. Developers can use the XDomainRequest object when cross-site security is not an issue.

Security Warning:   Cross-domain requests ("XDRs") are anonymous to protect user data. This means that servers cannot easily determine who is requesting data. To protect user privacy, respond with cross-domain data that is neither sensitive nor personally identifiable. To help prevent intranet data from being leaked to malicious Internet sites, we discourage intranet sites from making XDR data available.

Cross-domain requests require mutual consent between the document and the server. You can initiate a cross-domain request by creating an XDomainRequest (XDR) object with the window object, and opening a connection to a domain.

The document will request data from the domain's server by sending an Origin header with the value of the origin. It will only complete the connection if the server responds with an Access-Control-Allow-Origin header of either * or the exact URL of the requesting document. This behavior is part of the World Wide Web Consortium (W3C)'s Web Application Working Group's draft framework on client-side cross-domain communication that the XDomainRequest object integrates with.

For example, a server's Active Server Pages (ASP) page might include the following response header:

 
 
<% Response.AddHeader("Access-Control-Allow-Origin","*") %>

Cross domain requests can only be sent and received from a document to URLs in the following zones:

From Document \ To URL Intranet Trusted(Intranet) Trusted(Internet) Internet Restricted
Intranet Allow Allow Allow Allow Deny
Trusted(Intranet) Allow Allow Allow Allow Deny
Trusted(Internet) Deny Deny Allow Allow Deny
Internet Deny Deny Allow Allow Deny
Restricted Deny Deny Deny Deny Deny

The XDR protocol only works with the http:// and https:// protocols.

To use the XDR protocol, you first create an XDomainRequest object. Then you use the open method to establish a connection with a server. Once a connection is opened, the send method transmits data strings to the server for processing. For example:

 
 
// 1. Create XDR object:
var xdr = new XDomainRequest(); // 2. Open connection with server using GET method:
xdr.open("get", "http://www.contoso.com/xdr.aspx"); // 3. Send string data to server:
xdr.send();

Examples

The following example sends an empty message to a server of your choice. You can select a timeout value (default 10000 msec) when sending the request. When you click the Get button, the script creates aXDomainRequest, assigns event handlers, and initiates the request. Script alerts indicate how the request is progressing. Click the Stop button to cancel the request, or the Read button to view additional properties of the response, such as contentType and responseText.

 
<!DOCTYPE html>

<html>
<body>
<h2>XDomainRequest</h2>
<input type="text" id="tbURL" value="http://www.contoso.com/xdr.txt" style="width: 300px"><br>
<input type="text" id="tbTO" value="10000"><br>
<input type="button" onclick="mytest()" value="Get">&nbsp;&nbsp;&nbsp;
<input type="button" onclick="stopdata()" value="Stop">&nbsp;&nbsp;&nbsp;
<input type="button" onclick="readdata()" value="Read">
<br>
<div id="dResponse"></div>
<script>
var xdr;
function readdata()
{
var dRes = document.getElementById('dResponse');
dRes.innerText = xdr.responseText;
alert("Content-type: " + xdr.contentType);
alert("Length: " + xdr.responseText.length);
} function err()
{
alert("XDR onerror");
} function timeo()
{
alert("XDR ontimeout");
} function loadd()
{
alert("XDR onload");
alert("Got: " + xdr.responseText);
} function progres()
{
alert("XDR onprogress");
alert("Got: " + xdr.responseText);
} function stopdata()
{
xdr.abort();
} function mytest()
{
var url = document.getElementById('tbURL');
var timeout = document.getElementById('tbTO');
if (window.XDomainRequest)
{
xdr = new XDomainRequest();
if (xdr)
{
xdr.onerror = err;
xdr.ontimeout = timeo;
xdr.onprogress = progres;
xdr.onload = loadd;
xdr.timeout = tbTO.value;
xdr.open("get", tbURL.value);
xdr.send();
}
else
{
alert("Failed to create");
}
}
else
{
alert("XDR doesn't exist");
}
}
</script>
</body>
</html>

See also

window
XMLHttpRequest

最新文章

  1. L2-011. 玩转二叉树
  2. 裁剪要素出现错误 :HRESULT:0x80040239
  3. js生成[n,m]的随机数 以及实际运用
  4. commonlisp教程以及学习笔记-01
  5. window.opener强大功能
  6. System.Data.Entity 无法引用的问题
  7. 洛谷P2722 总分 Score Inflation
  8. Android -- 经验分享
  9. ZOJ3574(归并排序求逆数对)
  10. ASP.NET中XML转JSON的方法
  11. Java基础--定时任务Timer(转载)
  12. PredictionIO+Universal Recommender快速开发部署推荐引擎的问题总结(3)
  13. 如何优雅的写一个Vue 的弹框
  14. Python实战:网络爬虫都能干什么?
  15. PHP 将大量数据导出到 Excel 的方法
  16. 【Linux】使用ZStack私有云创建本地Linux服务器
  17. LeetCode题解之 Binary Tree Preorder Traversal
  18. 多线程学习-ListenableFuture使用介绍以及示例
  19. github fork 同步
  20. 85.YCbCr与YUV的区别

热门文章

  1. 20170609批量生成WORD合同
  2. 总结: MySQL(基础,字段约束,索引,外键,存储过程,事务)操作语法
  3. HDU-5001 Walk (概率DP)
  4. HDU 1710 二叉树遍历
  5. 找到多个与名为“Home”的控制器匹配的类型,如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间来搜索匹配此请求的
  6. Vue面试题整理
  7. selenium(五)伪造浏览器
  8. hdu 1253 胜利大逃亡 (代码详解)解题报告
  9. eclipse中使用Maven新建Servlet2.5的Web项目
  10. DIY微型操作系统(1)—— 开发的准备