六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 28|回复: 0

【转】XMLHttpRequest

[复制链接]

升级  72.67%

43

主题

43

主题

43

主题

秀才

Rank: 2

积分
159
 楼主| 发表于 2013-1-23 02:17:18 | 显示全部楼层 |阅读模式
The very first thing that we need to be able to do in order to retrieve something from the server to update the current web page (without reloading the whole page) is to create the object that will perform the processing for us. Modern browsers have a predefined class called XMLHttpRequest that we can use to define our object directly. Internet Explorer has provided five different interfaces to provide us with the functionality that we need each of which is accessed via AxtiveX. As each new version has improved the functioning of the object we want to grab the most recent version that is installed in the browser.
function createXMLHttp() {if (typeof XMLHttpRequest != 'undefined')return new XMLHttpRequest();else if (window.ActiveXObject) {var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp","MSXML2.XmlHttp.3.0";, "MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.5.0"];for (var i = avers.length -1; i >= 0; i--) {try {httpObj = new ActiveXObject(avers[i]);return httpObj;} catch(e) {}}}throw new Error('XMLHttp (AJAX) not supported');}var ajaxObj = createXMLHttp();



This code starts by checking if the XMLHttpRequest class exists. If it does then we use that to create our object. If it doesn't then we check if ActiveX is supported (which means that the browser is running Internet Explorer on Windows with ActiveX enabled). If it is then we try to create our object using the latest XMLHttp version (version 5.0). If that fails then we work our way back through the array trying each earlier version in turn until we find one that is installed in the browser and which therefore allows us to create our object. If all of those attempts fail or if neither XMLHttpRequest or ActiveX are supported then we throw an error to indicate that AJAX is not supported in this browser.
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表