并不是全部的浏览器都完整的实现了XMLHttpRequest 2 级的规范, 可是全部的浏览器都实现了它部分的规范。php
FormData
类型浏览器
append()
向其添加数据,包含两个参数:键和值;服务器
如:app
var data = new FormData(); data.append("name", "oliver");
也能够用表单元素的数据预先想其中填入键值对:ide
var data = new FormData(document.forms[0]);
它是为序列化表单以及建立于表单格式相同的数据提供了遍历:post
var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { console.log(xhr.responseText); } else { console.log("error"); } } }; xhr.open("post", "postexample.php", true); var form = document.getElementById("form1"); xhr.send(new FormData(form));
它的方便之处在于不用明确的在XHR对象上设置请求头部。code
IE8+惟一支持的超时设定事件,XHR对象的ontimeout事件。XHR对象的timeout设定超时时间,单位是毫秒数。这些设定要方法open以后,send以前。orm
var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { console.log(xhr.responseText); } else { console.log("error"); } } }; xhr.open("get", "getexample.php", true); xhr.timeout = 1000; xhr.ontimeout = function () { alert("Request did not return in a second."); }; xhr.send(null);
用于重写XHR响应的MIME类型。它能强迫服务器返回的数据类型给些为本方法提供的类型。使用方法:
在open以后,send以前。xml
xhr.open("get", "getexample.php", true); xhr.overrideMimeType("text/xml"); xhr.send(null);