i am trying to get a walkscore from the walkscore API. i make the ajax request to my php file, i get the response but i kee getting "Unexpected end of JSON input" when i try to alert the walkscore.
This is the PHP (if i test it directly through the URL, it gives me a perfectly json formatted result)
<?
function getWalkScore($lat, $lon, $address) {
$address=urlencode($address);
$url = "http://api.walkscore.com/score?format=json&address=$address";
$url .= "&lat=$lat&lon=$lon&wsapikey=KEY";
$str = @file_get_contents($url);
return $str;
}
$lat = $_GET['lat'];
$lon = $_GET['lon'];
$address = stripslashes($_GET['address']);
$json = getWalkScore($lat,$lon,$address);
echo $json;
?>
and this is my ajax call:
function loadStats() {
var geocoder = new google.maps.Geocoder;
var latlngStr = $("#loadStats").attr("data-latlng").split(',', 2);
var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
var reverseAddress='';
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
/*** Getting address needed for Walkability API call ***/
reverseAddress = results[1].formatted_address;
//alert (parseFloat(latlngStr[0]));
//alert(parseFloat(latlngStr[1]));
//alert(reverseAddress);
$.ajax({
type:"GET",
url:"scripts/walkscore.php",
data: {"address" : reverseAddress ,
"lat" : parseFloat(latlngStr[0]),
"lng" : parseFloat(latlngStr[1])
},
success: function(response){
var obj = $.parseJSON(response);
alert(obj.walkscore);
}
});
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
//var Lat=$("#loadStats").attr("data-latlng").slice(0, $("#loadStats").attr("data-latlng").indexOf(",")) ;
//var Lng=$("#loadStats").attr("data-latlng").slice($("#loadStats").attr("data-latlng").indexOf(",")+1,$("#loadStats").attr("data-latlng").lenght) ;
}
I don't know what i am doing wrong. this is the JSON i get from the API
{ "status": 1 , "walkscore": 4 , "description": "Car-Dependent" , "updated": "2016-04-04 21:15:44.699150" , "logo_url": "https://cdn.walk.sc/images/api-logo.png" , "more_info_icon": "https://cdn.walk.sc/images/api-more-info.gif" , "more_info_link": "https://www.redfin.com/how-walk-score-works" , "ws_link": "https://www.walkscore.com/score/Acres-homes-Houston-TX-USA/lat=29.852555/lng=-95.447845/?utm_source=cinquedomande.com&utm_medium=ws_api&utm_campaign=ws_api" , "help_link": "https://www.redfin.com/how-walk-score-works" , "snapped_lat": 29.8530 , "snapped_lon": -95.4480 }