js中回调函数

来源:互联网 发布:淘宝买身份证 编辑:程序博客网 时间:2024/04/30 12:33
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <script src="../js/jquery-1.8.1.min.js"></script></head><body>    <input type="button" id="ok" value="click me"/>    <script type="text/javascript">        //封装Ajax        var ajaxObj = {            xmlhttpresquest: '',            getXMLHttpRequest: function () {                var xmlHttp;                try{                    xmlHttp = new XMLHttpRequest();//FireFox,Opera 8.0,Safari                } catch (e) {                    try{                        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");//IE                    }                    catch (e) {                        try{                            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                        } catch (e) {                        }                    }                }                return xmlHttp;            },            post: function (ajaxJson) {                ajaxObj.xmlhttpresquest = ajaxObj.getXMLHttpRequest();                ajaxObj.xmlhttpresquest.onreadystatechange = function () {                    if (ajaxObj.xmlhttpresquest.readyState == 4) {//响应完毕                        if (ajaxObj.xmlhttpresquest.status == 200) {//成功响应                            //回调函数                            ajaxJson.callback(ajaxObj.xmlhttpresquest.responseText);                        }                    }                }                ajaxObj.xmlhttpresquest.open(ajaxJson.method, ajaxJson.url, true);                ajaxObj.xmlhttpresquest.send(ajaxJson.data);            }        };        window.onload = function () {            document.getElementById('ok').onclick = function () {                ajaxObj.post({                    method: 'post',                    url: 'Handler1.ashx',                    data: 'name=wj',                    callback: function (data) {                        alert('回调函数:'+data);                    }                });            }        }    </script></body></html>
0 0
原创粉丝点击