文章目录
Ajax常用库
jQuery中的ajax方法
// jquery中的ajax
$.ajax({
url: "http://localhost:3000/posts",
type: "get",
dataType: "json",
data: { "id": 5 },
beforeSend: function(xhr) { console.log(xhr); },
success: function(data) { console.log(data); },
error: function(xhr) { console.log(xhr); },
complete: function(xhr) { console.log(xhr); }
})
$.ajax()方法,传入对象的参数,执行异步HTTP请求
$.get()
GET快捷请求方法,格式$.get(url,data,callback)
$.get("http://localhost:3000/comments",
{ "id": 1}, function(data) {console.log(data);})
$.post()
POST快捷请求,格式$.post(url,data,callback)
$.ajax({
url: "http://localhost:3000/comments/4",
type: "put",
dataType: "json",
data: {"postId":2,"content": "goodbetterbest" },
success: function(data) { console.log(data);}
})
其他类型请求
put更改
$.ajax({
url: "http://localhost:3000/comments/4",
type: "put",
dataType: "json",
data: { "postId": 2, "content": "good"},
success: function(data) { console.log(data); }
})
delete删除
$.ajax({
url: "http://localhost:3000/comments/4",
type: "delete",
success: function(data) {
console.log(data);
}
})
jQuery其他方法
Axios介绍
Axios是目前应用最为广泛的AJAX封装库,同样的API、no