前后端交互

183 阅读1分钟

前后端交互:

前后端交互 添加数据:

            $.ajax({
                url:"http://localhost:3007/tag",
                type:"POST",
                data:{text:$("#text").val()},
                success:function(res){
                    if(res.status === 200){
                        load();
                   console.log("添加成功");
                }
                }
            })
            return false;
        })

运行如下: 输入111后,点击添加后显示出来

1.png

前后端交互 删除数据:

            var id = $(this).attr("data-id");
        $.ajax({
            url:"http://localhost:3007/tag",
            type:"DELETE",
            data:{_id: id},
            success: function(res){
                if(res.status === 200){
                   console.log("删除成功");
                }
            }
            })
        })

运行如下: 点击x删除,111删除

2.png

前后端交互 请求数据:

        $.ajax({
            url:"http://localhost:3007/tag",
            type:"GET",
            success:function(res){
                if(res.status===200){
                 const data = res.data;
                 let html = "";
                 for (let index = 0; index < data.length; index++) {
                     const element = data[index];
                     html += `<div class="alert alert-info pull-left">
            <button type="button" data-id="${element._id}" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <strong>${element.text}</strong>
        </div>`;
                 }
                 $(".col-xs-12").html(html);
                }
                console.log(res);
            },
            error:function(err){
                console.log(err);
            }
        })
}