我正在尝试从函数crimeTrendTable 中返回一些数据,该函数使用jquery .ajax 从Web 中提取一些数据,并将其返回到一个名为postcodeConvert 的函数中。
我想我需要实现一个回调函数以确保它只在返回数据后才返回信息,但我对如何做到这一点有点困惑?任何人都可以帮忙吗?
$(document).ready(function()
{
function crimeTrendTable(latitude, longitude){
//Find all available dates
availability_url = "http://policeapi2.rkh.co.uk/api/crimes-street-dates";
listings = $.ajax({
dataType: 'jsonp',
url: availability_url,
success: function(data){
latest = data[0]['date'];
three_months_date = data[3]['date'];
six_months_date = data[6]['date'];
year_ago_date = data[12]['date'];
list_dates = [latest, three_months_date, six_months_date, year_ago_date];
},
error: function(jqXHR, textStatus, errorThrown){
$('#results').html('<h2>Something went wrong!</h2><p><b>' + textStatus + '</b> ' + errorThrown + '</p>');
}
})
}
function postcodeConvert(entry){
//Convert to upper case
entry = entry.toUpperCase().replace(" ", "");
base_url = "http://mapit.mysociety.org/postcode/";
query_url = base_url+entry;
console.log(query_url);
$.getJSON(query_url, function(data){
$('div#results').html("<p>Latitude: "+data['wgs84_lat']+"</p><p>Longitude: "+data['wgs84_lon'])+"</p>";
data_results = "";
data_results = crimeTrendTable(data['wgs84_lat'], data['wgs84_lon']);
console.log("postcode"+data_results);
return data_results;
})
}
$('#postcode').focus(); //ensure the place field is the first box highlighted
//Disable the submit button from sending anything as we don't need to...
$('#form_submit').submit(function(){
return false;
});//end submit function
$(':submit').click(function(){
//function convert text box postcode into lat long
if ($('#postcode').val() !=''){
entry = $('#postcode').val();
postcodeConvert(entry);
}
});
});