5.2 Ajax 数据爬取实战
warning:
这篇文章距离上次修改已过187天,其中的内容可能已经有所变动。
// 使用jQuery发送Ajax请求获取网页内容
$.ajax({
url: 'https://example.com/data', // 目标网页或API的URL
type: 'GET', // 请求类型,根据需要也可以是'POST'
dataType: 'json', // 期望从服务器返回的数据类型
success: function(response) {
// 请求成功时的回调函数
console.log('数据爬取成功:', response);
// 处理数据,例如显示在页面上
// $('#result').text(JSON.stringify(response, null, 2));
},
error: function(xhr, status, error) {
// 请求失败时的回调函数
console.error('数据爬取失败:', status, error);
}
});
// 确保在DOM完全加载后执行上述代码
$(document).ready(function() {
// 你的代码可以放这里
});
这段代码演示了如何使用jQuery的$.ajax
方法来发送一个GET请求到指定的URL,并在请求成功时处理返回的数据。在实际应用中,你需要替换url
和数据处理部分以适应你的具体需求。
评论已关闭