$.ajax() 方法案例
【代码】$.ajax() 方法案例。
·
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.5.1.js" ></script>
<script>
$(function(){ // 创建一个页面就绪函数,其用途是在页面加载完毕后,执行这个function里面的代码;
$.ajax({
"url":"/ajax/news_list",
"type":"post",
"data":"t=tiobe",
"dataType":"json", // 在JavaScript中json的key可以不加引号,但是为了严谨和防止混乱,还是加上引号吧;
"success":function(json){
console.log(json);
for(var i=0;i<json.length;i++){
// append()的作用可以将append()中的html代码段追加到div的最后
$("#container").append("<h1>"+json[i].title+"</h1>");
}
},
"error":function(xmlhttp,errorText){ // xmlhttp和errorText哪儿来的???
console.log(xmlhttp);
console.log(errorText);
if(xmlhttp.status == "405"){
alert("无效的请求方式");
}else if(xmlhttp.status == "404"){
alert("未找到url资源");
}else if(xmlhttp.status == "500"){
alert("服务器内部错误");
}else{
alert("其他异常");
}
}
})
});
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)