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

html-css09

怎么获取通过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)

}

}

}

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

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

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

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

       alert(e)

     }})