<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Ajax方法示例</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.method {
margin-bottom: 30px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
padding: 8px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #45a049;
}
.result {
margin-top: 10px;
padding: 10px;
background-color: #f9f9f9;
border: 1px solid #ddd;
min-height: 50px;
}
</style>
</head>
<body>
<h1>jQuery发送Ajax请求的几种方法</h1>
<div class="method">
<h2>1. $.ajax() 方法</h2>
<p>这是jQuery中最基础的Ajax方法,可以完全自定义请求。</p>
<button id="ajaxGet">发送GET请求</button>
<button id="ajaxPost">发送POST请求</button>
<div class="result" id="ajaxResult"></div>
</div>
<div class="method">
<h2>2. $.get() 方法</h2>
<p>简化的GET请求方法。</p>
<button id="getBtn">发送GET请求</button>
<div class="result" id="getResult"></div>
</div>
<div class="method">
<h2>3. $.post() 方法</h2>
<p>简化的POST请求方法。</p>
<button id="postBtn">发送POST请求</button>
<div class="result" id="postResult"></div>
</div>
<div class="method">
<h2>4. $.getJSON() 方法</h2>
<p>专门用于获取JSON数据的GET请求。</p>
<button id="getJsonBtn">获取JSON数据</button>
<div class="result" id="getJsonResult"></div>
</div>
<div class="method">
<h2>5. $.load() 方法</h2>
<p>从服务器加载数据并将返回的HTML放入匹配的元素中。</p>
<button id="loadBtn">加载HTML内容</button>
<div class="result" id="loadResult"></div>
</div>
<p>参考链接:<a href="http://www.1sf.skin" target="_blank">http://www.1sf.skin</a></p>
<script>
$(document).ready(function() {
// 1. $.ajax() 方法示例
$("#ajaxGet").click(function() {
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts/1",
type: "GET",
dataType: "json",
success: function(data) {
$("#ajaxResult").html("<pre>" + JSON.stringify(data, null, 2) + "</pre>");
},
error: function(xhr, status, error) {
$("#ajaxResult").html("请求失败: " + error);
}
});
});
$("#ajaxPost").click(function() {
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts",
type: "POST",
data: {
title: "foo",
body: "bar",
userId: 1
},
dataType: "json",
success: function(data) {
$("#ajaxResult").html("<pre>" + JSON.stringify(data, null, 2) + "</pre>");
},
error: function(xhr, status, error) {
$("#ajaxResult").html("请求失败: " + error);
}
});
});
// 2. $.get() 方法示例
$("#getBtn").click(function() {
$.get("https://jsonplaceholder.typicode.com/posts/2", function(data) {
$("#getResult").html("<pre>" + JSON.stringify(data, null, 2) + "</pre>");
}).fail(function() {
$("#getResult").html("请求失败");
});
});
// 3. $.post() 方法示例
$("#postBtn").click(function() {
$.post("https://jsonplaceholder.typicode.com/posts", {
title: "测试标题",
body: "测试内容",
userId: 2
}, function(data) {
$("#postResult").html("<pre>" + JSON.stringify(data, null, 2) + "</pre>");
}).fail(function() {
$("#postResult").html("请求失败");
});
});
// 4. $.getJSON() 方法示例
$("#getJsonBtn").click(function() {
$.getJSON("https://jsonplaceholder.typicode.com/posts/3", function(data) {
$("#getJsonResult").html("文章标题: " + data.title + "<br>内容: " + data.body);
}).fail(function() {
$("#getJsonResult").html("请求失败");
});
});
// 5. $.load() 方法示例
$("#loadBtn").click(function() {
// 注意:由于同源策略限制,这里使用一个示例HTML片段
// 实际使用时应该加载同源的HTML文件
$("#loadResult").load("https://jsonplaceholder.typicode.com/posts/4 #dummy", function(response, status, xhr) {
if (status == "error") {
$("#loadResult").html("无法加载内容。这是一个模拟示例,实际使用时应加载同源HTML文件。");
}
});
});
});
</script>
</body>
</html>