一.jquery调用ajax方法
格式:$.ajax({});
参数:
type:请求方式GET/POST
url:请求地址url
async:是否异步,默认是true表示异步
data:发送到服务器的数据
dataType:预期服务器返回的数据类型
contentType:设置请求头
success:请求成功时调用此函数
error:请求失败时调用此函数
$.ajax({
type:"get/post", // 请求类型 GET/POST
url:"js/data.json", // 请求路径
dataType:"json", // 预期服务器返回的数据类型
data:{ // 请求参数,键值对的json对象
},
success:function(data){ // 请求成功时的回调函数
console.log(data);
}
});
二.$get()封装方法
1.无参数,无返回值
$.get("http://localhost:8080/jqueryAjax/ServletTest");
2.有参数,无返回值
$.get("http://localhost:8080/jqueryAjax/ServletTest",{uname:"zhangsan"});
3.无参数,有返回值
$.get("http://localhost:8080/jqueryAjax/ServletTest",function(data){
console.log(data);
});
4.有参数,有返回值
$.get("http://localhost:8080/jqueryAjax/ServletTest",{uname:"zhangsan"},function(data){
console.log(data);
});
$.get("js/data.json",{},function(data){
console.log(data);
});
三.$post()封装方法
1.无参数,无返回值
$.post("http://localhost:8080/jqueryAjax/ServletTest");
2.有参数,无返回值
$.post("http://localhost:8080/jqueryAjax/ServletTest",{uname:"zhangsan"});
3.无参数,有返回值
$.post("http://localhost:8080/jqueryAjax/ServletTest",function(data){
console.log(data);
});
4.有参数,有返回值
$.post("http://localhost:8080/jqueryAjax/ServletTest",{uname:"zhangsan"},function(data){
console.log(data);
});
$.post("js/data.json",{},function(data){
console.log(data);
});
四.$getJSON()封装方法
表示请求返回的数据类型是 JSON 格式的 ajax 请求
$.getJSON("http://localhost:8080/jqueryAjax/ServletTest",{uname:"zhangsan"},function(data){
console.log(data);
});