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

XDomainRequest object的更多相关文章

  1. Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]

    Browser security prevents a web page from making AJAX requests to another domain. This restriction i ...

  2. Enabling Cross-Origin Requests in ASP.NET Web API 2

    Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...

  3. HTTP访问控制(CORS)

    跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP请求.比如说,域名A(http://domaina.exampl ...

  4. Cross-origin resource sharing--reference

    Cross-origin resource sharing (CORS) is a mechanism that allows many resources (e.g., fonts, JavaScr ...

  5. HTTP Server to Client Communication

    1. Client browser short polling The most simple solution, client use Ajax to sends a request to the ...

  6. 跨域资源共享(CORS)--跨域ajax

    几年前,网站开发者都因为ajax的同源策略而撞了南墙.当我们惊叹于XMLHttpRequest对象跨浏览器支持所带来的巨大进步时,我们很快发现没有一个方法可以使我们用JavaScript实现请求跨域访 ...

  7. js中的跨域

    因为javascript的同源策略,导致它普通情况下不能跨域,直到现在,我还是不能完全理解js跨域的几种方法,没办法,只能慢慢学习,慢慢积累,这不,几天又在园里看到一篇博文,有所收获,贴上来看看; 原 ...

  8. CoreCLR源码探索(一) Object是什么

    .Net程序员们每天都在和Object在打交道 如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" 这个答案是对的 ...

  9. JavaScript Object对象

    目录 1. 介绍:阐述 Object 对象. 2. 构造函数:介绍 Object 对象的构造函数. 3. 实例属性:介绍 Object 对象的实例属性:prototype.constructor等等. ...

随机推荐

  1. 使用erlang实现简单的二进制通信协议

    最近实现的一种简单的协议以及工具,主要用于客户端服务端通讯传输二进制数据时,协议的解包与封包,具体如下:首先定义协议的格式,主要由三部分组成:        数据长度(数据部分长度+协议号长度):4个 ...

  2. Confluence 6 从外部目录中同步数据如何工作

    下面是有关缓存功能的一些摘要信息: 用户和用户组的缓存信息保存在应用程序的数据库中. 当你连接一个新的外部目录到系统中的时候,一个同步任务将会启动被,并且在后台运行拷贝所有需要的用户和用户组信息,以及 ...

  3. git 下载代码

    git clone https://github.com/ContextLogic/Wish-Merchant-API.git wish(wish是下载的地址,这样的话,就在你的住文件夹上)

  4. sql server数据库中char、nchar、varchar、nvarchar的选择

    在数据库中,字符型的数据是最多的,可以占到整个数据库的80%以上.为此正确处理字符型的数据,对于提高数据库的性能有很大的作用. 在字符型数据中,用的最多的就是Char与Varchar两种类型.前面的是 ...

  5. ORACLE常见方法使用(转)

    1.DBMS_LOB包的使用 2.如何释放DBMS_LOB.CREATETEMPORARY的空间 3.oracle数组

  6. Eclipse properties文件编辑插件

    安装 Properties Editor 步骤:help--->Install New Software...---> 名称:Properties Editor URL:http://pr ...

  7. OC 归档和解档

    #import <Foundation/Foundation.h> #define PATH @"/Users/mac/Desktop/file.txt" int ma ...

  8. JavaScript学习总结(十一)——Object类详解

    一.Object类介绍 Object类是所有JavaScript类的基类(父类),提供了一种创建自定义对象的简单方式,不再需要程序员定义构造函数. 二.Object类主要属性 1.constructo ...

  9. POJ 2896 AC自动机 or 暴力

    DESCRIPTION :大意是说.给你n个代表病毒的字符串.m个表示网站的字符串.让你计算有多少个网站被病毒感染了.被那些病毒感染了. 刚开始就想暴力.然而,忽略了条件:每个网站最多有三个病毒.于是 ...

  10. 合法的MAC地址

    以太网的地址为48位,由ieee统一分配给网卡制造商,每个网卡的地址都必须是全球唯一的.共6个字节的长度 字节 5 4 3 2 1 0  位 47..40 39..32 31..24 23..16 1 ...