注意:以下代码请在Firefox 3.5、Chrome 3.0、Safari 4之后的版本中进行测试。IE8的实现方法与其他浏览不同。

3,带验证信息的请求

身份验证是Web开发中经常遇到的问题,在跨域请求中,默认情况下是不发送验证信息的。要想发送验证信息,需要进行withCredentials 属性,下面就是一个简单请求的例子:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>孟宪会之AJAX跨域请求测试</title>
  6. </head>
  7. <body>
  8. <inputtype='button'value='开始测试'onclick='crossDomainRequest()'/>
  9. <divid="content"></div>
  10. <mce:scripttype="text/javascript"><!--
  11. var xhr =new XMLHttpRequest();
  12. var url ='http://dotnet.aspx.cc/RequestsWithCredentials.aspx';
  13. function crossDomainRequest() {
  14. document.getElementById("content").innerHTML ="开始进行请求……";
  15. if (xhr) {
  16. xhr.open('GET', url, true);
  17. xhr.onreadystatechange =handler;
  18. xhr.withCredentials ="true";
  19. xhr.send();
  20. } else {
  21. document.getElementById("content").innerHTML ="不能创建 XMLHttpRequest。";
  22. }
  23. }
  24. function handler(evtXHR) {
  25. if (xhr.readyState == 4) {
  26. if (xhr.status == 200) {
  27. var response =xhr.responseText;
  28. document.getElementById("content").innerHTML ="结果:" + response;
  29. } else {
  30. document.getElementById("content").innerHTML += "<br/>执行状态 status:" + xhr.status;
  31. }
  32. }
  33. else {
  34. document.getElementById("content").innerHTML += "<br/>执行状态 readyState:" + xhr.readyState;
  35. }
  36. }
  37. // --></mce:script>
  38. </body>
  39. </html>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>孟宪会之AJAX跨域请求测试</title>
  6. </head>
  7. <body>
  8. <input type='button' value='开始测试' onclick='crossDomainRequest()' />
  9. <div id="content"></div>
  10. <mce:script type="text/javascript"><!--
  11. var xhr = new XMLHttpRequest();
  12. var url = 'http://dotnet.aspx.cc/RequestsWithCredentials.aspx';
  13. function crossDomainRequest() {
  14. document.getElementById("content").innerHTML = "开始进行请求……";
  15. if (xhr) {
  16. xhr.open('GET', url, true);
  17. xhr.onreadystatechange = handler;
  18. xhr.withCredentials = "true";
  19. xhr.send();
  20. } else {
  21. document.getElementById("content").innerHTML = "不能创建 XMLHttpRequest。";
  22. }
  23. }
  24. function handler(evtXHR) {
  25. if (xhr.readyState == 4) {
  26. if (xhr.status == 200) {
  27. var response = xhr.responseText;
  28. document.getElementById("content").innerHTML = "结果:" + response;
  29. } else {
  30. document.getElementById("content").innerHTML += "<br/>执行状态 status:" + xhr.status;
  31. }
  32. }
  33. else {
  34. document.getElementById("content").innerHTML += "<br/>执行状态 readyState:" + xhr.readyState;
  35. }
  36. }
  37. // --></mce:script>
  38. </body>
  39. </html>

点击“开始测试”,我们可以检测到下面的请求执行过程:

  1. GET /RequestsWithCredentials.aspx HTTP/1.1
  2. Host: dotnet.aspx.cc
  3. 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)
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. Accept-Language: zh-cn,zh;q=0.5
  6. Accept-Encoding: gzip,deflate
  7. Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
  8. Keep-Alive: 300
  9. Connection: keep-alive
  10. Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
  11. Origin: http://www.meng_xian_hui.com:801
  12. HTTP/1.x 200 OK
  13. Date: Sun, 10 Jan 2010 14:12:26 GMT
  14. Server: Microsoft-IIS/6.0
  15. X-Powered-By: ASP.NET
  16. X-AspNet-Version: 2.0.50727
  17. Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
  18. Access-Control-Allow-Credentials: true
  19. Set-Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145;path=/; HttpOnly
  20. Set-Cookie: visit=1;expires=Sun, 10-Jan-2010 14:12:56 GMT;path=/
  21. Cache-Control: no-cache
  22. Pragma: no-cache
  23. Expires: -1
  24. Content-Type: text/html; charset=utf-8
  25. Content-Length: 1
  1. GET /RequestsWithCredentials.aspx HTTP/1.1
  2. Host: dotnet.aspx.cc
  3. 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)
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. Accept-Language: zh-cn,zh;q=0.5
  6. Accept-Encoding: gzip,deflate
  7. Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
  8. Keep-Alive: 300
  9. Connection: keep-alive
  10. Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
  11. Origin: http://www.meng_xian_hui.com:801
  12. HTTP/1.x 200 OK
  13. Date: Sun, 10 Jan 2010 14:12:26 GMT
  14. Server: Microsoft-IIS/6.0
  15. X-Powered-By: ASP.NET
  16. X-AspNet-Version: 2.0.50727
  17. Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
  18. Access-Control-Allow-Credentials: true
  19. Set-Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; path=/; HttpOnly
  20. Set-Cookie: visit=1; expires=Sun, 10-Jan-2010 14:12:56 GMT; path=/
  21. Cache-Control: no-cache
  22. Pragma: no-cache
  23. Expires: -1
  24. Content-Type: text/html; charset=utf-8
  25. Content-Length: 1

从上面的响应中可以看出,Cookie 是会随请求一起发送的。如果我们多次点击测试按钮,则可以看到请求和响应的结果是这样的:

  1. GET /RequestsWithCredentials.aspx HTTP/1.1
  2. Host: dotnet.aspx.cc
  3. 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)
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. Accept-Language: zh-cn,zh;q=0.5
  6. Accept-Encoding: gzip,deflate
  7. Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
  8. Keep-Alive: 300
  9. Connection: keep-alive
  10. Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
  11. Origin: http://www.meng_xian_hui.com:801
  12. Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145;visit=2
  13. HTTP/1.x 200 OK
  14. Date: Sun, 10 Jan 2010 14:13:58 GMT
  15. Server: Microsoft-IIS/6.0
  16. X-Powered-By: ASP.NET
  17. X-AspNet-Version: 2.0.50727
  18. Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
  19. Access-Control-Allow-Credentials: true
  20. Set-Cookie: visit=3;expires=Sun, 10-Jan-2010 14:14:28 GMT;path=/
  21. Cache-Control: no-cache
  22. Pragma: no-cache
  23. Expires: -1
  24. Content-Type: text/html; charset=utf-8
  25. Content-Length: 1
  1. GET /RequestsWithCredentials.aspx HTTP/1.1
  2. Host: dotnet.aspx.cc
  3. 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)
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. Accept-Language: zh-cn,zh;q=0.5
  6. Accept-Encoding: gzip,deflate
  7. Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
  8. Keep-Alive: 300
  9. Connection: keep-alive
  10. Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html
  11. Origin: http://www.meng_xian_hui.com:801
  12. Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; visit=2
  13. HTTP/1.x 200 OK
  14. Date: Sun, 10 Jan 2010 14:13:58 GMT
  15. Server: Microsoft-IIS/6.0
  16. X-Powered-By: ASP.NET
  17. X-AspNet-Version: 2.0.50727
  18. Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801
  19. Access-Control-Allow-Credentials: true
  20. Set-Cookie: visit=3; expires=Sun, 10-Jan-2010 14:14:28 GMT; path=/
  21. Cache-Control: no-cache
  22. Pragma: no-cache
  23. Expires: -1
  24. Content-Type: text/html; charset=utf-8
  25. Content-Length: 1

注意 Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; visit=2 这一行,访问计数器已经被一起发送到服务器。

4,IE8 中的实现方法

IE8已经开始支持跨域访问资源了,但是,IE8提供的功能还比较简单,可以进行简单的请求,下面是一个使用的例子:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>孟宪会之AJAX跨域请求测试</title>
  6. </head>
  7. <body>
  8. <inputtype='button'value='开始测试'onclick='crossDomainRequest()'/>
  9. <divid="content"></div>
  10. <mce:scripttype="text/javascript"><!--
  11. var xhr =new XDomainRequest();
  12. var url ='http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx';
  13. function crossDomainRequest() {
  14. document.getElementById("content").innerHTML ="开始……";
  15. if (xhr) {
  16. xhr.open('GET', url);
  17. xhr.onload =handler;
  18. xhr.send();
  19. } else {
  20. document.getElementById("content").innerHTML ="不能创建 XDomainRequest";
  21. }
  22. }
  23. function handler(evtXHR) {
  24. document.getElementById("content").innerHTML ="结果:" + xhr.responseText;
  25. }
  26. // --></mce:script>
  27. </body>
  28. </html>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>孟宪会之AJAX跨域请求测试</title>
  6. </head>
  7. <body>
  8. <input type='button' value='开始测试' onclick='crossDomainRequest()' />
  9. <div id="content"></div>
  10. <mce:script type="text/javascript"><!--
  11. var xhr = new XDomainRequest();
  12. var url = 'http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx';
  13. function crossDomainRequest() {
  14. document.getElementById("content").innerHTML = "开始……";
  15. if (xhr) {
  16. xhr.open('GET', url);
  17. xhr.onload = handler;
  18. xhr.send();
  19. } else {
  20. document.getElementById("content").innerHTML = "不能创建 XDomainRequest";
  21. }
  22. }
  23. function handler(evtXHR) {
  24. document.getElementById("content").innerHTML = "结果:" + xhr.responseText;
  25. }
  26. // --></mce:script>
  27. </body>
  28. </html>

另外,IE8的实现方法与其他浏览器不同。更多内容请参考 XDomainRequest 对象,地址是:
http://msdn.microsoft.com/zh-cn/library/cc288060(VS.85).aspx

最后,愿意测试的朋友可以访问这个 http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx 地址进行“简单请求”的测试,本页面允许任何地址进行跨域访问。(不好意思,个人网站可能被河蟹了)

AJAX(XMLHttpRequest)进行跨域请求方法详解(三)的更多相关文章

  1. AJAX(XMLHttpRequest)进行跨域请求方法详解

    AJAX(XMLHttpRequest)进行跨域请求方法详解(三) 2010年01月11日 08:48:00 阅读数:24213 注意:以下代码请在Firefox 3.5.Chrome 3.0.Saf ...

  2. AJAX(XMLHttpRequest)进行跨域请求方法详解(二)

    注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 2,预检请求 预检请求首先需要向另外一个域名的资源发送一个 HT ...

  3. AJAX(XMLHttpRequest)进行跨域请求方法详解(一)

    注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 跨域请求,顾名思义,就是一个站点中的资源去访问另外一个不同域名站 ...

  4. 在ASP.NET 5应用程序中的跨域请求功能详解

    在ASP.NET 5应用程序中的跨域请求功能详解 浏览器安全阻止了一个网页中向另外一个域提交请求,这个限制叫做同域策咯(same-origin policy),这组织了一个恶意网站从另外一个网站读取敏 ...

  5. JS JSOP跨域请求实例详解

    JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.这篇文章主要介绍了JS JSOP跨域请求实例详解的相关资料,需要的朋友可以参考下 ...

  6. JavaScript JSON AJAX 同源策略 跨域请求

    网页和Ajax和跨域的关系 1 Ajax使网页可以动态地.异步地的与服务器进行数据交互,可以让网页局部地与服务器进行数据交互 2 Ajax强调的是异步,但是会碰到跨域的问题. 3 而有很多技术可以解决 ...

  7. js中ajax如何解决跨域请求

    js中ajax如何解决跨域请求,在讲这个问题之前先解释几个名词 1.跨域请求 所有的浏览器都是同源策略,这个策略能保证页面脚本资源和cookie安全 ,浏览器隔离了来自不同源的请求,防上跨域不安全的操 ...

  8. VUE SpringCloud 跨域资源共享 CORS 详解

    VUE  SpringCloud 跨域资源共享 CORS 详解 作者:  张艳涛 日期: 2020年7月28日 本篇文章主要参考:阮一峰的网络日志 » 首页 » 档案 --跨域资源共享 CORS 详解 ...

  9. HTTP请求方法详解

    HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定 ...

随机推荐

  1. JavaScript(4)——闭包与this对象以及window对象

    闭包与this对象以及window对象 这次写的是这三个内容.其实在写之前,会觉得这三个内容很多,但是写了之后会发现,内容确实很多,但是可以写出来的也并不是很多.可能是我总结能力太差.但是这些内容我觉 ...

  2. AutoTile 自动拼接 番外篇(自动融合技术)

    http://pan.baidu.com/s/1dDQyfSl 密码:ttud 先睹为快吧. 之后 还差一个 智能替换 技术.

  3. 数组实现UITabview的cell设置

  4. 典型的DIV+CSS布局——左中右版式

    [效果] [HTML] <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Def ...

  5. python2.7学习记录

    一.两个学习网址(2.7已经过时,建议直接学习3) http://blog.csdn.net/longxibendi/article/details/41949215 http://www.liaox ...

  6. ASP.NET 修改密码代码

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  7. 移动端Click300毫秒点击延迟的来龙去脉(转)

    原文地址:What Exactly Is….. The 300ms Click Delay 快速响应是所有 UI 实现的重中之重.研究表明,当延迟超过 100 毫秒,用户就能感受到界面的卡顿. 然而, ...

  8. boolean类型相关

    如果逻辑对象无初始值或者其值为 0.-0.null."".false.undefined 或者 NaN,那么对象的值为 false.否则,其值为 true(即使当自变量为字符串 & ...

  9. hdu_5044_Tree(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5044 题意:给一棵树,在点和边上操作 题解:树链剖分,剖完后用树状数组维护即可,因为只有加减操作,连树 ...

  10. 运维必备:Oracle自备份精简教程(linux及win)

    Oracle在linux环境下的自动备份 1.自动导出及历史文件删除脚本 su - oracle<<EOF cd /db_backup/databak mv orabak*.* /db_b ...