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

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

     }})


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5905992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-07
下一篇 2023-03-07

发表评论

登录后才能评论

评论列表(0条)

保存