Ajax用法

Ajax是进行异步处理请求的,非常好用

<script>
        $(function () {
            $(".del").click(function () {
                var $this = $(this);
                var id = $(this).attr("data-id");
                //id=1&name=2 表单数据
                $.ajax({
                    type: "post",//规定传输方式
                    url: "./DelClass.ashx",//默认为当前页地址
                    data: { cid: id },//提交的数据
                    success: function (res) {
                        if (res == 1) {

                            $this.closest("tr").remove();
                        }
                        else {
                            alert(res);
                        }
                    },
                    error: function (response) {
                        console.log(response);
                    }
                });
            })
        });
    </script>

1,在$.ajax()方法中,包含
a. 请求参数列表data{ }
b. 成功回调函数success:function(data){ }

2,type 可用类型主要为post和get两种(默认为get)

a.get:从指定的资源请求数据(从服务器读取数据)
b.post:向指定的资源提交要被处理的数据(向服务器提交数据)

3,data 请求的数据,{ }中可以填入多项数据。如果不填(一般为get请求),则读取对应地址的全部数据,此时可以在console中通过console.log(res)显示数据情况。
4,success 和 error 两个函数 一般需要设置,方便确定请求是否成功,以及请求成功后的提示或是对数据的处理和显示。

 

posted @ 2021-11-25 17:29  顾屿南歌  阅读(59)  评论(0编辑  收藏  举报