使用jquery发送请求

1.发送get请求

1.1标准版发送get请求

let btn1 = $('#btn1');
    btn1.click(function () {
        $.ajax('http://localhost:3000/test_get', {
            method: 'GET',
            data: {name: '代罔', age: 18},
            success:function (result) {
                console.log(result);
            },
            error:function (err) {
                console.log(err);
            }
        })

1.2简化版发送get请求

 let btn1 = $('#btn1');
 btn1.click(function (){
     $.get('http://localhost:3000/test_get',{name:'代罔',age:18},(data)=>{console.log(data)});
 }

2.发送post请求

2.1.标准版发送post请求

 let btn2 = $('#btn2');
    btn2.click(function () {
        $.ajax('http://localhost:3000/test_post', {
            method: 'POST',
            data: {name: 'leilei', age: 18},
            success:function (result) {
                console.log(result);
            },
            error:function (err) {
                console.log(err);
            }
        })

2.2简化版发送post请求

let btn2 = $('#btn2');
 btn2.click(function () {
$.post('http://localhost:3000/test_post',{name:'Leilei',age: 18},(data)=>{
            console.log(data);
        })
    })
发布了29 篇原创文章 · 获赞 0 · 访问量 187

猜你喜欢

转载自blog.csdn.net/qq_41523392/article/details/103838529