一、创建XMLHTTP对像函数

?function createXMLHTTP () {
??? var request;
?????? var browser=navigator.appName; //获取浏览器信息
??? if (browser==”Microsoft Internet Explorer”){
??????????? request=new ActiveXObject(“Microsoft.XMLHttp”);
???return request;
???? }
???? //火狐及其它浏览器
??else {
???request=new XMLHttpRequest();
???return request;
???}?
??? }
????

二、异步发送函数
? function Send_Request(url){
?? //OPEN 函数建立连接,一定要异步方式
?? xml_http.open(“GET”,url,true);
?? //声明回调函数
?? xml_http.onreadystatechange=processRequest;
?? //发送请求
?? xml_http.send(null);
?? }
?

三、回调函数
? function processRequest(){
?? if (xml_http.readyState==4){
??? if (xml_http.status==200){
???? //获取服务器响应文本
???? var ResponseStr=xml_http.responseText;
???? alert (ResponseStr);
???? }
???? else
???? {
????? alert (“页面发生异常”);
????}
??? }
?? }

记住,在使用时一定要实例化全局对像,如下:

var xml_http=createXMLHTTP();?//建立AJAX 全局对像

否则将无法使用

自由转载,转载请注明: 转载自WEB开发笔记 www.chhua.com

本文链接地址: Ajax必须的三个函数(Ajax三步走) http://www.chhua.com/web-note309

相关笔记

更多