XDomainRequest object
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 |
|---|---|
|
Returns a reference to the constructor of an object. |
|
|
Gets the Content-Type property in the HTML request or response header. |
|
|
Retrieves the response body as a string. |
|
|
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">
<input type="button" onclick="stopdata()" value="Stop">
<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
XDomainRequest object的更多相关文章
- 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 ...
- 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 ...
- HTTP访问控制(CORS)
跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP请求.比如说,域名A(http://domaina.exampl ...
- Cross-origin resource sharing--reference
Cross-origin resource sharing (CORS) is a mechanism that allows many resources (e.g., fonts, JavaScr ...
- HTTP Server to Client Communication
1. Client browser short polling The most simple solution, client use Ajax to sends a request to the ...
- 跨域资源共享(CORS)--跨域ajax
几年前,网站开发者都因为ajax的同源策略而撞了南墙.当我们惊叹于XMLHttpRequest对象跨浏览器支持所带来的巨大进步时,我们很快发现没有一个方法可以使我们用JavaScript实现请求跨域访 ...
- js中的跨域
因为javascript的同源策略,导致它普通情况下不能跨域,直到现在,我还是不能完全理解js跨域的几种方法,没办法,只能慢慢学习,慢慢积累,这不,几天又在园里看到一篇博文,有所收获,贴上来看看; 原 ...
- CoreCLR源码探索(一) Object是什么
.Net程序员们每天都在和Object在打交道 如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" 这个答案是对的 ...
- JavaScript Object对象
目录 1. 介绍:阐述 Object 对象. 2. 构造函数:介绍 Object 对象的构造函数. 3. 实例属性:介绍 Object 对象的实例属性:prototype.constructor等等. ...
随机推荐
- CentOS7.6 yum install Git
1. yum install git 2. git version or git –version 3. uninstall: git remove
- 『Sklearn』特征向量化处理
『Kaggle』分类任务_决策树&集成模型&DataFrame向量化操作 1 2 3 4 5 6 7 8 9 '''特征提取器''' from sklearn.feature_extr ...
- 『Pandas』数据读取&DataFrame切片
读取文件 numpy.loadtxt() import numpy as np dataset_filename = "affinity_dataset.txt" X = np.l ...
- python-day33--Process类中的方法及属性
p.daemon = True -->守护进程,守护进程不可以再有子进程,并且主进程死守护进程就死,要写在p.start()之前 p.join() ---> 主进程等子进程执行完 之后再结 ...
- Linux 强制安装rpm 包
Linux 强制安装rpm 包 2014年12月12日 10:21 [root@ilearndb1 Server]# rpm -ivh unixODBC-devel-2.* --nodeps -- ...
- dp练习(4)——过河卒
1010 过河卒 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如图,A ...
- MySQL 事件跟踪器
/*第一步 创建以下两个 日志表 */ CREATE TABLE `slow_log` ( `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMEST ...
- 浅谈musql中using的使用---高性能(三)
转载地址:https://blog.csdn.net/lppl010_/article/details/79429657 mysql中using的用法为: using()用于两张表的join查询,要求 ...
- AOJ1024 Cleaning Robot 2.0
先说一说这个OJ:貌似是11区某大学ACM的OJ,叫AIZU ONLINE JUDGE,貌似还可以看到部分犇的代码...跪跪跪 然后知道这个OJ是某场比赛安利的= = 接下来将做法: 首先我们可以发现 ...
- Python Django 之 Template 模板语言简介
一.什么事模板语言 html+逻辑控制语句 二.模板语言的作用 帮助前端处理后端发来的数据,方便前端展示(杂糅渲染) 三.模板语言语法 1.{{变量}} 变量使用双大括号{{}} 2.万能的句点号. ...