在没有jquery的情况下调用aspx中的泛型处理程序

时间:2011-02-18 06:37:31

标签: javascript

我可以在不使用jQuery的情况下在aspx中调用Generic处理程序吗?

1 个答案:

答案 0 :(得分:0)

是的,jQuery本身就是一个javascript,所以你可以通过编写自己的脚本来调用。使用jQuery的好处是你不需要担心浏览器的兼容性,必须编写更少的代码,可以利用jquery插件等,只需添加jQuery.js的成本。

示例:

<script type="text/javascript">
    // Intialize XMLHTTP object
    function GetXmlHttpObject() {
        var A;
        if (window.XMLHttpRequest) {
            A = new XMLHttpRequest();
        } else {
            var msxmlhttp = new Array(
        'Msxml2.XMLHTTP.6.0',
        'Msxml2.XMLHTTP.3.0',
        'Msxml2.XMLHTTP',
        'Microsoft.XMLHTTP');
            for (var i = 0; i < msxmlhttp.length; i++) {
                try {
                    A = new ActiveXObject(msxmlhttp[i]);
                    break;
                } catch (e) {
                    A = null;
                }
            }
        }

        return A;
    }


    function sendAjaxRequest(url, postVars, callbackFunc) {
        var isPost = (postVars && postVars.length > 0);

        var xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }


        if (!isPost) {
            if (url.indexOf('?') == -1) {
                url += '?' + uncache();
            } else {
                url += '&' + uncache();
            }
        }
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) { //This function will execute on receive
                var callback;
                var extra_data = false;

                var data = xmlHttp.responseText;

                callback = callbackFunc;
                callbackFunc(data, extra_data);
            }
        };        

        //Send data to the url through ajax
        if (isPost) {
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-Length", postVars.length);
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.send(postVars);
        } else {
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        }
    }

    function uncache() {
        var d = new Date();
        var time = d.getTime();

        return 'time=' + time;
    }

    function comp(result) {
        alert(result);
    }

    sendAjaxRequest('test.html', '', comp);


</script>