SpringBoot之RESTFul风格的接口调用(jQuery-Ajax)

 

一、Get

$.ajax({
    type: "get",
    url: "url地址",
    async: true,
    dataType:"json",//返回数据类型
    success: function(data){
        console.log(data);
    }
});

 

二、Post

var data = {
    "id": "",
    "name": "",
    ...
};

$.ajax({
    type:"post",
    url: "url地址",
    async:true,
    data: JSON.stringify(data),//后台接受时使用了@RequestBody注解,需要Json字符串和指定contentType
    contentType:"application/json;charset=utf-8",
    dataType: "json",
    success: function(data){
        alert("数据准备: successful");
        console.log(data);
    }
});

三、Put(待测试)

四、Delete

var data = {
    "id": "",
    "name": "",
    ...
};
    
$.ajax({
    type:"DELETE",
    url: "url地址",
    async:true,
    contentType:"application/json",//设置请求参数类型为json字符串
    data:JSON.stringify(data),//将json对象转换成json字符串发送
    dataType:"json",
    success:function(result){
        alert(result);
    }

});

 

posted @ 2019-03-29 23:25  拂衣志  阅读(201)  评论(0编辑  收藏  举报