当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQueryAJAX最常用的三种方法列举介绍

jQueryAJAX最常用的三种方法列举介绍

2019年02月26日  | 移动技术网IT编程  | 我要评论

cv大法用习惯了,有时候都忘记基本的写法了,做个记录,方便下次查看

其实jquery 调用ajax方法有很多,如下图, 不过在项目中常用的也就那么几个,做个小统计:

\

最常用的三种:

<script>
    //把所有需要用到的地址归类到一个对象里
    var weburl = {
        "show1url": "{{ url('address/list1') }}",
        "show2url": "{{ url('address/list2') }}",
        "show3url": "{{ url('address/list3') }}"
    };

    function getdata() {
        $.get(weburl.show1url,  //获取地址
            function(json){
                console.log(json);
             });
    }


    function postdata(v1) {
        $.ajaxsettings.async = true;   //在这里设置同步或异步 默认为true(可不写)  false为同步

        $.post(weburl.show2url,   //获取地址
            {
                "id":v1          //需要传输的数据
            },
            function(json){
                console.log(json);
            });
    }

    function fulldata(id) {
        $.ajax({              //  ajax 请求设置。所有选项都是可选的。
            async:false,        //请求是同步或异步    默认为true  为true时不用写
            type: "post",            //设置类型
            url: weburl.show3url,           //数据传输地址
            datatype: "json",               //获取的数据类型
            data: {"id":id},            //传参
            success: function (json) {      //请求成功之后调用
                // console.log(json);
                console.log(json);
            },
            error: function () {        //请求出错时调用
                console.log("请求失败");
            }
        })
    }


</script>

最后一种虽然很全,不过如果功能要求不是特别复杂的用前两个就ok

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网