ajax学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax学习笔记相关的知识,希望对你有一定的参考价值。

1. ajax_func.js的代码如下 :

false;
技术分享//定义可复用的http请求发送函数,初始化、指定处理函数、发送请求的函数
技术分享function send_request(method, url, content, responseType, callback) 技术分享{
技术分享    http_request = false;
技术分享    //开始初始化XMLHttpRequest对象
技术分享    if(window.XMLHttpRequest) 技术分享{
技术分享        //Mozilla浏览器
技术分享        http_request = new XMLHttpRequest();
技术分享        if(http_request.overrideMimeType) 技术分享{
技术分享            //设置MIME类别
技术分享            http_request.overrideMimeType("text/xml");
技术分享        }
技术分享    } else if(window.ActiveXObject) 技术分享{
技术分享        //IE浏览器
技术分享        try 技术分享{
技术分享            http_request = new ActiveXObject("Msxml2.XMLHTTP");
技术分享        }
技术分享        catch (e) 技术分享{
技术分享            try 技术分享{
技术分享                http_request = new ActiveXObject("Microsoft.XMLHTTP");
技术分享            }
技术分享            catch (e)技术分享{}
技术分享        }
技术分享    }
技术分享
技术分享    if(!http_request) 技术分享{
技术分享        //异常,创建对象实例失败
技术分享        window.alert("不能创建XMLHttpRequest对象实例.");
技术分享        return false;
技术分享    }
技术分享
技术分享    if(responseType.toLowerCase() == "text" || responseType.toLowerCase() == "xml") 技术分享{
技术分享        http_request.onreadystatechange = callback;
技术分享    }  else 技术分享{
技术分享        window.alert("响应类别参数错误.");
技术分享        return false;
技术分享    }
技术分享
技术分享    //确定发送请求的方式和URL以及是否一步执行下段代码
技术分享    if(method.toLowerCase() == "get") 技术分享{
技术分享        http_request.open(method, url, true);
技术分享    } else if(method.toLowerCase() == "post") 技术分享{
技术分享        http_request.open(method, url, true);
技术分享        http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
技术分享    } else 技术分享{
技术分享        window.alert("http请求类别参数错误.");
技术分享        return false;
技术分享    }
技术分享
技术分享    http_request.send(content);
技术分享}

2. 回调函数举例
1)处理返回文本格式信息的函数举例(调用send_request方法时responseType为text)

{
技术分享    if(http_request.readyState == 4) 技术分享{
技术分享        if(http_request.status == 200) 技术分享{
技术分享            //信息已经成功返回,开始处理信息
技术分享            alert("Text文档相应.");
技术分享        } else 技术分享{
技术分享            alert("您所请求的页面有异常.");
技术分享        }
技术分享    }
技术分享}

2)处理返回格式信息的函数举例(调用send_request方法时responseType为xml)
function processXMLResponse() {
技术分享    if(http_request.readyState == 4) {
技术分享        if(http_request.status == 200) {
技术分享            //信息已经成功返回,开始处理信息
技术分享            alert("XML响应.");
技术分享        } else {
技术分享            alert("您所请求的页面有异常.");
技术分享        }
技术分享    }
技术分享}

以上是关于ajax学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

Ajax学习笔记

ajax学习笔记

AJAX学习笔记

ajax学习笔记

Ajax学习笔记

Ajax学习笔记