var req = new ActiveXObject("Microsoft.XMLHTTP");
对其他的浏览器,使用:
var req = new XMLHttpRequest();
你当然想在代码中实现一些判断逻辑,有很多方法可以做到这点,但是我倾向于简单的方案,比如只是检查一下某个对象是否存在:
var req;
if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
}
不管你怎么实现,上面的代码执行之后,你会发现变量req现在指向了一个XMLHttpRequest对象,这个对象有一组属性和方法,列举如下:
Property Description
onreadystatechange Event handler for an event that fires at every state change
readyState Status:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
responseText Data returned from server in string form
responseXML DOM-compatible document object of data returned
status HTTP status code (i.e., 200, 404, 500, etc.)
statusText