日期:2014-05-16  浏览次数:20718 次

使用原始Ajax访问与使用jquery的Ajax访问的实例
原始Ajax的访问

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht
ml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery Ajax</title>
    <script type="text/javascript">
        $(function()
        {
            var xhr = new AjaxXmlHttpRequest();
            $("#btnAjaxOld").click(function(event)
            {
                var xhr = new AjaxXmlHttpRequest();
                xhr.onreadystatechange = function()
                {
                    if (xhr.readyState == 4)
                    {
                        document.getElementById("divResult").innerHTML = xhr.responseText;
                    }
                }
                xhr.open("GET", "data/AjaxGetCityInfo.aspx?resultType=html", true);
                xhr.send(null);
            });
        })

        //跨浏览器获取XmlHttpRequest对象
        function AjaxXmlHttpRequest()
        {
            var xmlHttp;
            try
            {
                // Firefox, Opera 8.0+, Safari
                xmlHttp = new XMLHttpRequest();
            }
            catch (e)
            {

                // Internet Explorer
                try
                {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
                {

                    try
                    {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e)
                    {
                        alert("您的浏览器不支持AJAX!");
                        return false;
                    }
                }
            }
            return xmlHttp;
        }        
    </script>

</head>
<body>    
    <button id="btnAjaxOld">原始Ajax调用</button><br />
    <br />
    <div id="divResult"></div>
</body>
</html>


jquery的访问
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm
l1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery Ajax</title>
    <script type="text/javascript" src="scripts/jquery-1.3.2-vsdoc2.js"></script>
    <script type="text/javascript">
        $(function()
        {            
            $("#btnGetHtml").click(function(event)
            {
                $("#divResult").load("data/AjaxGetCityInfo.aspx", { "resultType": "html" });
            });       
        })        
    </script>
</head>
<body>    
    <button id="btnAjaxJquery">使用jQuery的load方法</button>
    <br />
    <div id="divResult"></div>
</body>
</html>