怎么获取通过ajax请求的html代码

html-css06

怎么获取通过ajax请求的html代码,第1张

利用回调函数获取服务器返回的结果

JavaScript code

var xmlHttp

function createXMLHttpRequest() {

if(window.XMLHttpRequest) {

xmlHttp = new XMLHttpRequest()

} else if (window.ActiveXObject) {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")

}

}

function routeList(){

createXMLHttpRequest()

url = "manage_action_class.php?"&ran="+Math.random() //后端请求页面

method = "GET" //传输方式

xmlHttp.open(method,url,true)

xmlHttp.onreadystatechange = showList //这里为回调函数

xmlHttp.send(null)

}

function showList(){

if (xmlHttp.readyState == 4){

if (xmlHttp.status == 200){

var text = xmlHttp.responseText //这里获得服务器返回的数据

document.getElementById("route").innerHTML = text //将数据放入html指定div中

}else {

alert("response error code:"+xmlHttp.status)

}

}

}

<script type="text/javascript">

        $(document).ready(function() {

            $("#select").click(function() {

                var startdate = $("#startdate").datebox('getValue')

                var endate = $("#enddate").datebox('getValue')

                $.ajax({

                    type: "post",

                    dataType: "html",

                    url: "../Service/WebService.ashx?Method=GetMealCalendHtml",

                    data: { startDate: startdate, endDate: endate },

                    success: function(json) {

                        $("#calendaPanel").empty()

                        $("#calendaPanel").append(json)

                    }

                })

            })

        })

        function Oncheck(mealdata) {

            $("#" + mealdata.id + " input[type=checkbox]").each(function() { console.log(this) })

        }  

    </script>

ajax是jquery框架中的一个方法,主要用于异步传输数据

ajax可以通过success回调函数得到返回结果

具体步骤请参考以下代码块

$.ajax({ url: "test.html", context: document.body, success: function(e){

       alert(e)

     }})