129.JQuery中使用AJAX

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JQuery中使用AJAX</title>
    <!-- 网络导入jQuery -->
    <script src="http://code.jquery.com/jquery-3.6.0.js"></script>

</head>
<body>
    <h1>JQuery中使用AJAX</h1>
    <input type="button" value="发送get请求" onclick="test_get()">
    <input type="button" value="发送post请求" onclick="test_post()"><br>
    <input type="button" value="发送get请求json" onclick="test_get_args_json()">
    <input type="button" value="发送get请求str" onclick="test_get_args_str()"><br>
    <input type="button" value="发送post请求json" onclick="test_post_args_json()">
    <input type="button" value="发送post请求str" onclick="test_post_args_str()">
    <input type="button" value="发送post请求放入data" onclick="test_post_json()"><br>
    <input type="button" value="发送请求之前beforesend" onclick="test_beforsend()"><br>
    <script>
        // get请求访问
        function test_get() {
            $.ajax({
                type:'get',
                url:'http://httpbin.org/get',
                success:(data)=>{
                    console.log(data);
                }
            })
        }

        // post请求访问
        function test_post() {
            $.ajax({
                type:'post',
                url:'http://httpbin.org/post',
                success:(data)=>{
                    console.log(data);
                }
            })
        }

        // json形式传递
        function test_get_args_json() {
            $.ajax({
                type:'get',
                url:'http://httpbin.org/get',
                // 传递请求参数,会自动拼接 : "url": "http://httpbin.org/get?name=sxt&pwd=123"
                data:{
                    name:'sxt',
                    pwd:123
                },
                success:(data)=>{
                    console.log(data);
                }
            })
        }

        // str形式传递
        function test_get_args_str() {
            $.ajax({
                type:'get',
                url:'http://httpbin.org/get',
                // 传递请求参数,会自动拼接 : "url": "http://httpbin.org/get?name=sxt&pwd=123"
                data:'name=sxt&pwd=123',
                success:(data)=>{
                    console.log(data);
                }
            })
        }

        // json形式传递
        function test_post_args_json() {
            $.ajax({
                type:'post',
                url:'http://httpbin.org/post',
                // 同下,会隐藏请求参数,显示在form中
                //form: {name: 'sxt', pwd: '123'} url: "http://httpbin.org/post"
                data:{
                    name:'sxt',
                    pwd:123
                },
                success:(data)=>{
                    console.log(data);
                }
            })
        }

        // str形式传递
        function test_post_args_str() {
            $.ajax({
                type:'url: "http://httpbin.org/post"',
                url:'http://httpbin.org/post',
                data:'name=sxt&pwd=123',
                success:(data)=>{
                    console.log(data);
                }
            })
        }

        // 默认情况下data数据放在form下,把data数据以json格式放入data中
        function test_post_json() {
            $.ajax({
                type:'post',
                url:'http://httpbin.org/post',
                data:JSON.stringify({
                    name:'sxt',
                    pwd:123
                }),
                contentType:'application/json',
                success:(data)=>{
                    console.log(data);
                }
            })
        }
    
        function test_beforsend() {
            $.ajax({
                type:'get',
                url:'http://httpbin.org/get',
                // 传递请求参数,会自动拼接 : "url": "http://httpbin.org/get?name=sxt&pwd=123"
                data:{
                    name:'sxt',
                    pwd:123
                },
                success:(data)=>{
                    console.log(data);
                },
                beforeSend:()=>{
                    console.log("这是发送请求之前先处理的内容");
                    // 可以在此函数中控制传送内容,若返回的是false将不会发送请求
                    return false
                }
            })
        }
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想成为数据分析师的开发工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值