douxun4173 2016-05-06 21:38 采纳率: 0%
浏览 56

walkcore API JSON错误

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 }
  • 写回答

1条回答 默认 最新

  • doula4096 2016-05-07 00:00
    关注

    I figured it out. I was stupid enough to pass lng instead of lon to the php script.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程