jQuery下的ajax请求html文件
var mainUrl = ‘./aaa.html'
$.ajax({
type: 'GET', //提交数据的类型 POST GET
url: mainUrl, //提交的网址// 此处mainUrl为本人定义
dataType: "html", //"xml", "html", "script", "json", "jsonp", "text".
beforeSend: function () {
requesting();// 请求前的动画事件
},
//成功返回之后调用的函数
success: function (response) {
// console.log(response)
$("#main").html(" ");
$("#main").html(response);// 请求到的response添加到<div id="mian">的dom元素中
},
//调用出错执行的函数
error: function (error) {
console.log("error");//请求出错处理
notfound();
}
});
本文介绍如何使用jQuery的AJAX方法发起GET请求获取HTML文件,并在请求成功后将响应内容加载到指定DOM元素中。示例代码展示了设置请求类型、URL、数据类型、请求前动画、响应处理及错误处理的方法。
633

被折叠的 条评论
为什么被折叠?



