写篇文章让自己清楚Ajax——(3)Jquery库的Ajax

100 阅读1分钟

Jquery是曾经前端开发常用的js库,特别是ajax的封装,十分具有学习意义。

官方文档

快速上手

$.ajax

最简单的get请求写法:

        $.ajax({
            type: "GET",
            url: "http://127.0.0.1:7001/axios?name=13",
            success: function(res){
                console.log(res)
            }
        });

post最简写法:

        $.ajax({
            type: "POST",
            url: "http://127.0.0.1:7001/axios",
            data:{
                name:12346,
            },
            success: function(res){
                console.log(res)
            }
        });

$.get

参数一:url地址 ; 参数二:get参数 ; 参数三:回调函数

$.get 是简化的 $.ajax写法,专门的get请求

$.get( "http://127.0.0.1:7001/axios?name=13", { name:14, id:'juejinjin' } )

携带在url后的参数会覆盖参数二中的同键名属性。服务器打印参数:

{ name: '13', id: 'juejinjin' }

完整结构写法:

        $.get("http://127.0.0.1:7001/axios", {
            name: 14,
            id: 'juejinjin'
        },res=>{
            console.log(res)
        })

$.post

参数一:url ; 参数二:

你会发现仍然是简单化的写法:(get能够通过url携带参数)

$.post("test.php", { name: "John", time: "2pm" } );

完整写法:

        $.post("http://127.0.0.1:7001/axios", {
            name: 14,
            id: 'juejinjin'
        },res=>{
            console.log(res)
        })

详细补充

请求类型大小写不敏感。