Javascript $.getJSON返回未识别的
我试图从GoogleMaps提供的JSON访问持续时间数据。用户输入2个邮政编码,I应显示所用时间Javascript $.getJSON返回未识别的,javascript,json,jquery,google-maps-api-3,Javascript,Json,Jquery,Google Maps Api 3,我试图从GoogleMaps提供的JSON访问持续时间数据。用户输入2个邮政编码,I应显示所用时间 function getData() { var origin = $("#origin_pc").val() + ",australia"; var destination = $("#dest_pc").val() + ",australia"; // var origin="3135,australia"; // var destination="3155,a
function getData() {
var origin = $("#origin_pc").val() + ",australia";
var destination = $("#dest_pc").val() + ",australia";
// var origin="3135,australia";
// var destination="3155,australia";
url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
return url;
}
function calculateDistance() {
// console.log(getData());
var my_json;
$.getJSON(getData(), function (json) {
my_json = json;
});
console.log(my_json.routes[0].legs[0].duration.value);
}
这是我的HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="text" id="origin_pc" />
<input type="text" id="dest_pc" />
<input type="submit" onclick="calculateDistance();">
</form>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="distance.js"></script>
</body>
</html>
我对生成的URL进行了处理,打开后,它给出了正确的JSON对象。return的786
您需要在getJSON回调函数中使用my_json
$.getJSON(getData(), function(json) {
my_json = json;
$('body').text(my_json.routes[0].legs[0].duration.value);
});