jquery发送ajax请求

jquery发送ajax请求

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta name="generator" content="editplus" />
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
    <meta charset="UTF-8">
    <title>jquery发送ajax请求</title>

</head>

<body>
    <style>
        #div_one {
            width: 100px;
            height: 100px;
            border: 1px solid #123
        }
    </style>
    <div>
        <h2>
            发送jq ajax的请求
        </h2>
        <button>get</button>
        <button>post</button>
        <button>通用型方法ajax</button>
        <div id="div_one"></div>

    </div>

    <script>
        //get请求发送
        const divone = document.getElementById("div_one")
        $('button').eq(0).click(function() {
                //$.get("url","data参数",'回调函数',响应体类型)       
                $.get('http://127.0.0.1:8000/jq', {
                    name: '周杰伦',
                    age: '41'
                }, function(data) {
                    console.log(data)
                    $("#div_one").html(data.name + data.age)
                }, "json")
            })

        //post
        $('button').eq(1).click(function() {
            //$.get("url","data参数",'回调函数')
            $.post('http://127.0.0.1:8000/jq', {
                name: '周杰伦',
                age: '41'
            }, function(data) {
                console.log(data)
                    //  $("#div_one").html(data.name) //jq方法
                divone.innerHTML = data.age + data.name //js方法
            }, "json")
        })

        //AJax请求方法
        $('button').eq(2).click(function() {
            $.ajax({
                //url
                url: 'http://127.0.0.1:8000/jq',
                //参数
                data: {
                    a: 100,
                    b: 100
                },
                //请求类型
                type: 'GET',
                //响应体类型
                dataType: 'json',
                //成功的回调
                success: function(data) {
                    console.log(data)
                    $("#div_one").html(data.name + data.age)
                },
                //超时时间
                timeout: 2000,
                //失败的回调
                error: function() {
                    console.log("出错了")
                    $("#div_one").html("出错了")
                }
            })
        })
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海鸥002

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

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

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

打赏作者

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

抵扣说明:

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

余额充值