Javascript $.getJSON返回/范围问题?
这是我的代码,警报始终显示null作为项中的值Javascript $.getJSON返回/范围问题?,javascript,jquery,getjson,Javascript,Jquery,Getjson,这是我的代码,警报始终显示null作为项中的值 function make_recent_item(recent_counter){ var json_path_popular= 'http://localhost/complaints_old/webservice/get_new_complaints' + '?recent_counter='+recent_counter; var item=null; $.getJSON( json_path_popular
function make_recent_item(recent_counter){
var json_path_popular= 'http://localhost/complaints_old/webservice/get_new_complaints' + '?recent_counter='+recent_counter;
var item=null;
$.getJSON( json_path_popular, function( data ){
item=
'<div class="item list">'+
'<div class="image">'+
'<div class="quick-view" '+data.data[0].complaint_id+'><i class="fa fa-eye"></i><span>Quick View</span></div>'+
'<div href="item-detail.html">'+
'<img style="width:260px;height:195px;" src="data:image/jpg;base64,'+ data.data[0].picture + '"/>'+
'</div>'+
'</div>'+
'<div class="wrapper">'+
'<a href="item-detail.html"><h3>Cash Cow Restaurante</h3></a>'+
'<figure>'+data.data[0].municipality_name+'</figure>'+
'<div class="info">'+
'</div>'+
'</div>'+
'</div>';
});
alert(item);
return item;
}
函数make\u recent\u项目(recent\u计数器){
var json_path_http://localhost/complaints_old/webservice/get_new_complaints“+”?最近的计数器=“+最近的计数器;
var项=null;
$.getJSON(json\u路径\u流行,函数(数据){
项目=
''+
''+
“快速查看”+
''+
''+
''+
''+
''+
''+
“”+数据。数据[0]。市政府名称+“”+
''+
''+
''+
'';
});
警报(项目);
退货项目;
}
我希望返回HTML(字符串格式),以便在$(“#”)中使用它。在(项)之后。$。getJSON()
异步返回结果返回
$.getJSON()
从函数中,使用.then()
链接到函数调用,包括.fail()
链接到.then()
处理$.getJSON()
返回的可能错误
函数make\u recent\u项目(recent\u计数器){
var json_path_http://localhost/complaints_old/webservice/get_new_complaints“+”?最近的计数器=“+最近的计数器;
return$.getJSON(json\u path\u popular)
}
使最近的项目(/*参数*/)
.then(功能(数据){
变量项=“”+
''+
“快速查看”+
''+
''+
''+
''+
''+
''+
“”+数据。数据[0]。市政府名称+“”+
''+
''+
''+
'';
//用“物品”做东西`
$(项目)。附于(“正文”);
})
.fail(函数(jqxhr、textStatus、errorshown){
日志(textStatus,errorshown);
});
function make_recent_item(recent_counter) {
var json_path_popular= 'http://localhost/complaints_old/webservice/get_new_complaints' + '?recent_counter='+recent_counter;
return $.getJSON(json_path_popular)
}
make_recent_item(/* parameter */)
.then(function( data ){
var item ='<div class="item list">'+
'<div class="image">'+
'<div class="quick-view" '+data.data[0].complaint_id+'><i class="fa fa-eye"></i><span>Quick View</span></div>'+
'<div href="item-detail.html">'+
'<img style="width:260px;height:195px;" src="data:image/jpg;base64,'+ data.data[0].picture + '"/>'+
'</div>'+
'</div>'+
'<div class="wrapper">'+
'<a href="item-detail.html"><h3>Cash Cow Restaurante</h3></a>'+
'<figure>'+data.data[0].municipality_name+'</figure>'+
'<div class="info">'+
'</div>'+
'</div>'+
'</div>';
// do stuff with `item`
$(item).appendTo("body");
})
.fail(function(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
});