// 方法一:
$.ajax({
url:'./a.json',
type:'GET',
// dataType:'json',
success:function(data){
console.log(data);
}
});
//ajax:url,请求的类型,成功的回调函数
// 方法二 原生
var xhr = new XMLHttpRequest();//创造出来一台手机
xhr.open("GET",'./a.json');//拨号
//打电话
xhr.send();
xhr.onreadystatechange = function(){
//只有请求状态是4,状态码是200
if(xhr.readyState === 4 && xhr.status === 200){
console.log(JSON.parse(xhr.responseText));
}
}
// 方法三:
$.get('./a.json',function(data){
console.log(data);
},'json');
// 方法四
$.getJSON('./a.json',function(data){
console.log(data);
$.getJSON('./b.php' + data.str,function(){
$.getJSON
});
});
总要的东西发多次请求嵌套在里面
// 不重要的东西 --> 构造第二次请求 --> 构造第三次请求
// $.getJSON('./a.json').then(function(data){
// console.log(data);
// });
// 方式五
fetch('./a.json',{method:'get'}).then(function(response){
response.json().then(function(data){
console.log(data);
})
});
$.ajax({
url:'./a.json',
type:'GET',
// dataType:'json',
success:function(data){
console.log(data);
}
});
//ajax:url,请求的类型,成功的回调函数
// 方法二 原生
var xhr = new XMLHttpRequest();//创造出来一台手机
xhr.open("GET",'./a.json');//拨号
//打电话
xhr.send();
xhr.onreadystatechange = function(){
//只有请求状态是4,状态码是200
if(xhr.readyState === 4 && xhr.status === 200){
console.log(JSON.parse(xhr.responseText));
}
}
// 方法三:
$.get('./a.json',function(data){
console.log(data);
},'json');
// 方法四
$.getJSON('./a.json',function(data){
console.log(data);
$.getJSON('./b.php' + data.str,function(){
$.getJSON
});
});
总要的东西发多次请求嵌套在里面
// 不重要的东西 --> 构造第二次请求 --> 构造第三次请求
// $.getJSON('./a.json').then(function(data){
// console.log(data);
// });
// 方式五
fetch('./a.json',{method:'get'}).then(function(response){
response.json().then(function(data){
console.log(data);
})
});