AJAX(XMLHttpRequest)进行跨域请求方法详解(三)
注意:以下代码请在Firefox 3.5、Chrome 3.0、Safari 4之后的版本中进行测试。IE8的实现方法与其他浏览不同。
3,带验证信息的请求
身份验证是Web开发中经常遇到的问题,在跨域请求中,默认情况下是不发送验证信息的。要想发送验证信息,需要进行withCredentials 属性,下面就是一个简单请求的例子:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>孟宪会之AJAX跨域请求测试</title>
- </head>
- <body>
- <inputtype='button'value='开始测试'onclick='crossDomainRequest()'/>
- <divid="content"></div>
- <mce:scripttype="text/javascript"><!--
- var xhr =new XMLHttpRequest();
- var url ='http://dotnet.aspx.cc/RequestsWithCredentials.aspx';
- function crossDomainRequest() {
- document.getElementById("content").innerHTML ="开始进行请求……";
- if (xhr) {
- xhr.open('GET', url, true);
- xhr.onreadystatechange =handler;
- xhr.withCredentials ="true";
- xhr.send();
- } else {
- document.getElementById("content").innerHTML ="不能创建 XMLHttpRequest。";
- }
- }
- function handler(evtXHR) {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- var response =xhr.responseText;
- document.getElementById("content").innerHTML ="结果:" + response;
- } else {
- document.getElementById("content").innerHTML += "<br/>执行状态 status:" + xhr.status;
- }
- }
- else {
- document.getElementById("content").innerHTML += "<br/>执行状态 readyState:" + xhr.readyState;
- }
- }
- // --></mce:script>
- </body>
- </html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>孟宪会之AJAX跨域请求测试</title>
- </head>
- <body>
- <input type='button' value='开始测试' onclick='crossDomainRequest()' />
- <div id="content"></div>
- <mce:script type="text/javascript"><!--
- var xhr = new XMLHttpRequest();
- var url = 'http://dotnet.aspx.cc/RequestsWithCredentials.aspx';
- function crossDomainRequest() {
- document.getElementById("content").innerHTML = "开始进行请求……";
- if (xhr) {
- xhr.open('GET', url, true);
- xhr.onreadystatechange = handler;
- xhr.withCredentials = "true";
- xhr.send();
- } else {
- document.getElementById("content").innerHTML = "不能创建 XMLHttpRequest。";
- }
- }
- function handler(evtXHR) {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- var response = xhr.responseText;
- document.getElementById("content").innerHTML = "结果:" + response;
- } else {
- document.getElementById("content").innerHTML += "<br/>执行状态 status:" + xhr.status;
- }
- }
- else {
- document.getElementById("content").innerHTML += "<br/>执行状态 readyState:" + xhr.readyState;
- }
- }
- // --></mce:script>
- </body>
- </html>
点击“开始测试”,我们可以检测到下面的请求执行过程:
- GET /RequestsWithCredentials.aspx HTTP/1.1
- Host: dotnet.aspx.cc
- User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Language: zh-cn,zh;q=0.5
- Accept-Encoding: gzip,deflate
- Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
- Keep-Alive: 300
- Connection: keep-alive
- Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
- Origin: http://www.meng_xian_hui.com:801
- HTTP/1.x 200 OK
- Date: Sun, 10 Jan 2010 14:12:26 GMT
- Server: Microsoft-IIS/6.0
- X-Powered-By: ASP.NET
- X-AspNet-Version: 2.0.50727
- Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
- Access-Control-Allow-Credentials: true
- Set-Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145;path=/; HttpOnly
- Set-Cookie: visit=1;expires=Sun, 10-Jan-2010 14:12:56 GMT;path=/
- Cache-Control: no-cache
- Pragma: no-cache
- Expires: -1
- Content-Type: text/html; charset=utf-8
- Content-Length: 1
- GET /RequestsWithCredentials.aspx HTTP/1.1
- Host: dotnet.aspx.cc
- User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Language: zh-cn,zh;q=0.5
- Accept-Encoding: gzip,deflate
- Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
- Keep-Alive: 300
- Connection: keep-alive
- Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
- Origin: http://www.meng_xian_hui.com:801
- HTTP/1.x 200 OK
- Date: Sun, 10 Jan 2010 14:12:26 GMT
- Server: Microsoft-IIS/6.0
- X-Powered-By: ASP.NET
- X-AspNet-Version: 2.0.50727
- Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
- Access-Control-Allow-Credentials: true
- Set-Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; path=/; HttpOnly
- Set-Cookie: visit=1; expires=Sun, 10-Jan-2010 14:12:56 GMT; path=/
- Cache-Control: no-cache
- Pragma: no-cache
- Expires: -1
- Content-Type: text/html; charset=utf-8
- Content-Length: 1
从上面的响应中可以看出,Cookie 是会随请求一起发送的。如果我们多次点击测试按钮,则可以看到请求和响应的结果是这样的:
- GET /RequestsWithCredentials.aspx HTTP/1.1
- Host: dotnet.aspx.cc
- User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Language: zh-cn,zh;q=0.5
- Accept-Encoding: gzip,deflate
- Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
- Keep-Alive: 300
- Connection: keep-alive
- Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
- Origin: http://www.meng_xian_hui.com:801
- Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145;visit=2
- HTTP/1.x 200 OK
- Date: Sun, 10 Jan 2010 14:13:58 GMT
- Server: Microsoft-IIS/6.0
- X-Powered-By: ASP.NET
- X-AspNet-Version: 2.0.50727
- Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
- Access-Control-Allow-Credentials: true
- Set-Cookie: visit=3;expires=Sun, 10-Jan-2010 14:14:28 GMT;path=/
- Cache-Control: no-cache
- Pragma: no-cache
- Expires: -1
- Content-Type: text/html; charset=utf-8
- Content-Length: 1
- GET /RequestsWithCredentials.aspx HTTP/1.1
- Host: dotnet.aspx.cc
- User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Language: zh-cn,zh;q=0.5
- Accept-Encoding: gzip,deflate
- Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
- Keep-Alive: 300
- Connection: keep-alive
- Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
- Origin: http://www.meng_xian_hui.com:801
- Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; visit=2
- HTTP/1.x 200 OK
- Date: Sun, 10 Jan 2010 14:13:58 GMT
- Server: Microsoft-IIS/6.0
- X-Powered-By: ASP.NET
- X-AspNet-Version: 2.0.50727
- Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
- Access-Control-Allow-Credentials: true
- Set-Cookie: visit=3; expires=Sun, 10-Jan-2010 14:14:28 GMT; path=/
- Cache-Control: no-cache
- Pragma: no-cache
- Expires: -1
- Content-Type: text/html; charset=utf-8
- Content-Length: 1
注意 Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; visit=2 这一行,访问计数器已经被一起发送到服务器。
4,IE8 中的实现方法
IE8已经开始支持跨域访问资源了,但是,IE8提供的功能还比较简单,可以进行简单的请求,下面是一个使用的例子:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>孟宪会之AJAX跨域请求测试</title>
- </head>
- <body>
- <inputtype='button'value='开始测试'onclick='crossDomainRequest()'/>
- <divid="content"></div>
- <mce:scripttype="text/javascript"><!--
- var xhr =new XDomainRequest();
- var url ='http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx';
- function crossDomainRequest() {
- document.getElementById("content").innerHTML ="开始……";
- if (xhr) {
- xhr.open('GET', url);
- xhr.onload =handler;
- xhr.send();
- } else {
- document.getElementById("content").innerHTML ="不能创建 XDomainRequest";
- }
- }
- function handler(evtXHR) {
- document.getElementById("content").innerHTML ="结果:" + xhr.responseText;
- }
- // --></mce:script>
- </body>
- </html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>孟宪会之AJAX跨域请求测试</title>
- </head>
- <body>
- <input type='button' value='开始测试' onclick='crossDomainRequest()' />
- <div id="content"></div>
- <mce:script type="text/javascript"><!--
- var xhr = new XDomainRequest();
- var url = 'http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx';
- function crossDomainRequest() {
- document.getElementById("content").innerHTML = "开始……";
- if (xhr) {
- xhr.open('GET', url);
- xhr.onload = handler;
- xhr.send();
- } else {
- document.getElementById("content").innerHTML = "不能创建 XDomainRequest";
- }
- }
- function handler(evtXHR) {
- document.getElementById("content").innerHTML = "结果:" + xhr.responseText;
- }
- // --></mce:script>
- </body>
- </html>
另外,IE8的实现方法与其他浏览器不同。更多内容请参考 XDomainRequest 对象,地址是:
http://msdn.microsoft.com/zh-cn/library/cc288060(VS.85).aspx
最后,愿意测试的朋友可以访问这个 http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx 地址进行“简单请求”的测试,本页面允许任何地址进行跨域访问。(不好意思,个人网站可能被河蟹了)
AJAX(XMLHttpRequest)进行跨域请求方法详解(三)的更多相关文章
- AJAX(XMLHttpRequest)进行跨域请求方法详解
AJAX(XMLHttpRequest)进行跨域请求方法详解(三) 2010年01月11日 08:48:00 阅读数:24213 注意:以下代码请在Firefox 3.5.Chrome 3.0.Saf ...
- AJAX(XMLHttpRequest)进行跨域请求方法详解(二)
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 2,预检请求 预检请求首先需要向另外一个域名的资源发送一个 HT ...
- AJAX(XMLHttpRequest)进行跨域请求方法详解(一)
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 跨域请求,顾名思义,就是一个站点中的资源去访问另外一个不同域名站 ...
- 在ASP.NET 5应用程序中的跨域请求功能详解
在ASP.NET 5应用程序中的跨域请求功能详解 浏览器安全阻止了一个网页中向另外一个域提交请求,这个限制叫做同域策咯(same-origin policy),这组织了一个恶意网站从另外一个网站读取敏 ...
- JS JSOP跨域请求实例详解
JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.这篇文章主要介绍了JS JSOP跨域请求实例详解的相关资料,需要的朋友可以参考下 ...
- JavaScript JSON AJAX 同源策略 跨域请求
网页和Ajax和跨域的关系 1 Ajax使网页可以动态地.异步地的与服务器进行数据交互,可以让网页局部地与服务器进行数据交互 2 Ajax强调的是异步,但是会碰到跨域的问题. 3 而有很多技术可以解决 ...
- js中ajax如何解决跨域请求
js中ajax如何解决跨域请求,在讲这个问题之前先解释几个名词 1.跨域请求 所有的浏览器都是同源策略,这个策略能保证页面脚本资源和cookie安全 ,浏览器隔离了来自不同源的请求,防上跨域不安全的操 ...
- VUE SpringCloud 跨域资源共享 CORS 详解
VUE SpringCloud 跨域资源共享 CORS 详解 作者: 张艳涛 日期: 2020年7月28日 本篇文章主要参考:阮一峰的网络日志 » 首页 » 档案 --跨域资源共享 CORS 详解 ...
- HTTP请求方法详解
HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定 ...
随机推荐
- ASP.NET中application对象的用法(面试题)
ASP.NET中application对象的用法 本文导读:Application对象是HttpApplicationState类的一个实例,Application状态是整个应用程序全局的.Appli ...
- SpringMvc+thymeleaf+HTML5中文乱码问题
SpringMvc+thymeleaf+HTML5环境下遇到中文乱码...... 按照以往经验逐个排查,开发环境统一为utf-8编码,服务器也配置了编码过滤器,tomcat也是utf-8编码.前台页面 ...
- Canvas基础讲义
今天先花点简单的篇幅和大家介绍下canvas. 1. 基本篇 1.1. 什么是 Canvas canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 H ...
- Webdriver实现对菜单栏的灵活切换功能,附上代码,类似的菜单栏切换可以自己封装
有时一级菜单下可能会有二级菜单,这时就需要对其下面的元素进行判断,如果使用webdriver原生的方法去获取未知的元素进行判断,显然是不可能的,因为webdriver本身就是基于明确的元素进行定位的, ...
- android配置文件详解
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...
- foreach 跟volist 有什么区别?
<foreach name="list" item="vo">{$vo.id}{$vo.name}</foreach>Foreach标签 ...
- a/b + c/d
a/b + c/d Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- PHP:class const
const变量经常被当做常量用在php的类中,隐含的意思是这个变量是常量,不能被修改.编译器会自动检测,如果被赋值会被提示错误警告. 正确实例1: <?php class test { cons ...
- rebot framework的搭建 在windows下
Robot Framework 介绍 Robot Framework 是一款基于 Python 的功能自动化测试框架.它具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进 ...
- 经常出现null错误之tostring
如果需要转换的类型可能为null,如果使用tostring就可能引发错误,这时候可以使用convert.tostring方法.