ajax之XMLHttpRequest在CHROME的测试

var hr=null;
function http(){
if(window.XMLHttpRequest)
hr = new XMLHttpRequest();
else hr = new ActiveXObject(“Microsoft.XMLHTTP”);
hr.onreadystatechange=function()
{

if(hr.readyState==1) console.log(“服务器连接已建立”);
if(hr.readyState==2) console.log(“请求已接受”);
if(hr.readyState==3) console.log(“请求处理中”);
if (hr.readyState==4 && hr.status==200)
{
console.log(“请求已完成,且响应已就绪”);
document.write(hr.responseText); }
}
hr.open(“GET”,”http://blog.sina.com.cn”,true);
hr.send();
}
http();

XMLHttpRequest 增强功能

来源:http://technet.microsoft.com/zh-cn/ie/hh673569

Internet Explorer 10 和使用 JavaScript 的 Windows 应用商店应用引入了对于 XMLHttpRequest 对象的若干增强功能,能够为不断出现的标准提供更好的支持并且更容易支持常见的开发者方案。 这些方案包括:

  • 下载和上载图像、视频和音频等二进制文件(无需使用插件)。
  • 下载多媒体内容流(无需使用插件)。
  • 改进了对 XMLHttpRequest 操作状态的解读。
  • 改进了与其他浏览器的互操作性。

这些更改将在以下各节中详细介绍:

二进制对象的上载和下载

Internet Explorer 10 使 XMLHttpRequest 扩展为可支持二进制数据。 通过增加对 文件 API 规范的 Blob 接口的支持,此功能已部分完成。

当一个 XMLHttpRequest 对象的 responseType 属性被设置为“blob”时,与该请求关联的数据将作为二进制数据进行处理。 这会影响下载请求的响应属性的值(例如,GET)。

下面的示例演示如何使用 XMLHttpRequest 将图像下载到 Blob 对象,然后将结果分配到网页上的图像元素。


<span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> req = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">new</span> XMLHttpReqest(); 
xhr.open(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"GET"</span>, <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"download?name="</span> + name, true); 
xhr.responseType = <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"blob"</span>; 
xhr.onreadystatechange = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span> () <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span> (xhr.readyState == xhr.DONE) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> blob = xhr.reponse; 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> image = document.getElementById(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"my-image"</span>); 
    image.addEventListener(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"load"</span>, <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span> (evt) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
      URL.revokeObjectURL(evt.target.src); 
    <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
    image.src = URL.createObjectURL(blob); 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
xhr.send();

当下载请求的 responseType 属性被设置为 “ms-stream” 时,内容能够被即时处理。如以下示例所示。


<span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> xhr = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">new</span> XMLHttpReqest(); 
xhr.open(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"GET"</span>, <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"download?name="</span> + name, true); 
xhr.responseType = <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"ms-stream"</span>; 
xhr.onreadystatechange = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span> () <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span> (xhr.readyState == xhr.LOADING) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> stream = xhr.reponse; 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> video = document.getElementById(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"my-video"</span>); 
    video.addEventListener(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"loadeddata"</span>, <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span> (evt) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
      URL.revokeObjectURL(evt.target.src); 
    <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
    video.src = URL.createObjectURL(stream); 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
xhr.send();

彗星流支持

通过允许在上载对 XMLHttpRequest 请求的响应的同时读取 responseText 属性,Internet Explorer 10 可支持多段 HTTP 流(又称 彗星流),如以下示例所示:


xhr.open(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"GET"</span>, url); 
xhr.timeout = timeout; 
xhr.onreadystatechange = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span>() <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span> (<span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.readyState &gt;= <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">3</span> &amp;&amp; <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.status == <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">200</span>) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> content = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.responseText; 
    handleContent(content); 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
xhr.send();

在较早版本的 Windows Internet Explorer 中,只有在 readyState 属性被设置为“done”时,才能读取 responseText 属性。

请注意, responseText 属性将返回到目前为止响应读取的完整值。 如果你希望处理单个数据包(与接收到它们时一样),则可以使用 progress 事件(详见下一节)或跟踪在readyStateChange 事件的每个迭代中的 responseText 的长度,如以下示例所示。


xhr.open(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"GET"</span>, url); 
xhr.timeout = timeout; 
xhr.onreadystatechange = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span>() <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span> (<span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">typeof</span> <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.index == <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"undefined"</span>) 
    <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.index = <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">0</span>; 
     
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span> (<span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.readyState &gt;= <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">3</span> &amp;&amp; <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.status == <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">200</span>) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> content = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.responseText; 
    handleContent( content.substring(<span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.index) ) 
    <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.index = content.length; 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
xhr.send();

增强的事件支持

Internet Explorer 10 将 XMLHttpRequest 对象扩展为可支持下列在 XMLHttpRequest 级别 2 规范中定义的事件:

事件 描述

loadstart

在请求开始时触发。

progress

在请求发送或接收数据期间,在服务器指定的时间间隔触发。

abort

在请求被取消时触发,例如,在调用 abort() 方法时。

error

在请求失败时触发。

load

在请求成功完成时触发。

timeout

在作者指定的时间段已经结束时触发。

loadend

在请求完成时触发,无论请求是成功还是失败。

XMLHttpRequest 对象的事件处理遵循在 DOM 事件级别 3 规范和 进度事件规范中指定的模型,如以下示例所示。


<span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> xhr = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">new</span> XMLHttpRequest(); 
<span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> url = <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"some-url"</span>; 
 
xhr.timeout = <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">5000</span>; 
xhr.addEventListener(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"timeout"</span>, handleTimeout(evt), false); 
 
xhr.onprogress = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">function</span>(evt) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  handleEvent(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"data: "</span> + <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.responseText); 
  <span class="js__sl_comment" style="font-weight: inherit; font-style: inherit; color: green;">// Calculate progress </span> 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> str = <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">""</span>; 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span> (evt.lengthComputable) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> percent = <span class="js__num" style="font-weight: inherit; font-style: inherit; color: red;">100</span> * evt.loaded / evt.total; 
    str = percent + <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"%. Current total size: "</span> + <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.responseText.length); 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">else</span> <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    str = <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"Progress unknown. Current total size: "</span> + <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">this</span>.responseText.length; 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
  updateProgress(str); 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
xhr.open(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"GET"</span>, url); 
xhr.send();

XMLHttpRequest 的跨源资源共享 (CORS)

Internet Explorer 10 添加了与 XMLHttpRequest (XHR)对象相关的跨源资源共享 (CORS) 的支持。 根据 跨源资源共享规范中的定义,CORS 使用 HTTP 头启用跨域 Web 请求,这通常受到同一站点源策略的限制。

默认情况下,同一站点源策略会阻止网站从其他域的服务器中请求资源。但是,如果相应的管理员选择允许此类请求,则支持 XMLHttpRequest (XHR) 请求的 CORS 的浏览器就可以访问其他域的资源。

当一个网页发出 XHR 请求时,Internet Explorer 发送一个源头数据到目标服务器;该头数据包含请求的协议方案(http:// 或 https://)和发出请求的网页的主机名。 如果目标服务器批准了请求,它将返回一个 Access-Control-Allow-Origin 头数据,并允许请求被处理。

XMLHttpRequest 对象现在支持 withCredentials 属性,它允许 XHR 请求以包含授权机制。右关更多信息,请参阅 XMLHttpRequest 级别 2 规范。

withCredentials 属性可用于检测 CORS 支持,如以下示例所示。


<span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> url = <span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"http://contoso.com/services/"</span> 
<span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span>( window.XMLHttpRequest ) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">var</span> oReq = <span class="js__operator" style="font-weight: bold; font-style: inherit; color: #4169e1;">new</span> XMLHttpRequest(); 
  <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">if</span>( oReq.withCredentials == true ) <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
    oReq.open(<span class="js__string" style="font-weight: inherit; font-style: inherit; color: teal;">"GET"</span>, url, true); 
    oReq.onload = handleResponse(); 
    oReq.send( null ); 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">else</span> <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span> 
  <span class="js__sl_comment" style="font-weight: inherit; font-style: inherit; color: green;">// CORS not support.  Handle fallback</span> 
  <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span> <span class="js__statement" style="font-weight: bold; font-style: inherit; color: navy;">else</span> <span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">{</span>  
  <span class="js__sl_comment" style="font-weight: inherit; font-style: inherit; color: green;">// XMLHttpRequest not supported; handle fallback</span> 
<span class="js__brace" style="font-weight: bold; font-style: inherit; color: red;">}</span>