jQuery中ajax的使用方式
$.ajax({
url: address,
type: 'GET',
async: true,
dataType: 'json',
success: function (data) {
if (data.returnCode == 0) {
compileTemplate(data.beans[0]);
sessionStorage.setItem('currentCity', data.beans[0].currentCity);
sessionStorage.setItem('nearCity', data.beans[0].nearCity);
}
},
error: function (xhr, textStatus) {
console.log(xhr);
console.log(textStatus);
}
});
使用延迟函数
$.ajax({
url: '../../../mockConfig.json',
type: 'GET',
async: true,
dataType: 'json',
}).done(function(data){
var urlheaderLeftTotal = '../../../data/' + data.headerLeftTotal;
$.ajax({
url:urlheaderLeftTotal,
type:'GET',
async: true,
dataType: 'json'
}).done(function (data){
if(data.returnCode==0){
console.log(data.beans);
var headerLeftTotalText=Handlebars.compile(headerLeftTotalTemplate)(data);
$('#zheng-ul').html(headerLeftTotalText);
}
}).done(function(data){
console.log(data.beans[0].icon)
$.ajax({
url: '../../../data/'+data.beans[0].icon+'.json',
type: 'GET',
async: true,
dataType: 'json',
}).done(function (data) {
console.log(data);
var headerRightDetailText=Handlebars.compile(headerRightDetail)(data);
console.log(headerRightDetailText);
$('#zheng-meishi').html(headerRightDetailText);
})
});
});