jQuery发送Ajax请求的几种方法示例

<!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>
    
    

    <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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值