2011-11-17 55 views
-2

我想要这个代码给alert用户使用他们的透视地址,但由于某种原因它没有执行。有人能指引我正确的方向吗?javascript无法执行mapquest api

谢谢!

<html> 
<head> 
<title>geoload</title> 
<script type="text/javascript" src="http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_DEV KEY GOES HERE=40.0755&lng=-76.329999&output=json&callback=renderGeocode"></script> 

<script type="text/javascript"> 
function renderGeocode(response){ 
    var location = response.results[0].locations[0]; 
    alert(location.adminArea5 + ", " + location.adminArea4); 
} 
</script> 
</head> 
<body onload=(load"renderGeocode")></body> 
</html> 
+3

location.adminArea5我认为你错过了一个“+” – chchrist

+0

你在哪里调用这个函数? – locrizak

+0

我看到回调,但你在哪里传递任何东西?我不是任何方式的JavaScript大师,但它会在我看来像你正在调用一个不存在的功能。 – Jared

回答

3

你已经错过了+标志:

alert(location.adminArea5 + ", " + location.adminArea4); 
         ^
+0

感谢您的回复。我添加了+符号,并且还添加了onload =“load(renderGeocode)”,但仍然没有运气。 – Felix

1

身体的onload不是必需的,因为在调用mapquestapi包括回调参数。回调参数是renderGeocode,它将调用同名的javascript函数。见下面的代码:

<html> 
<head> 
<title>geoload</title> 
<script type="text/javascript" > 
function renderGeocode(response) { 
    console.log(response) 
} 

</script> 
<script type="text/javascript" src="http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_DEV KEY GOES HERE&lat=40.0755&lng=-76.329999&callback=renderGeocode" ></script> 
</head> 
<body></body> 
</html> 
+0

你应该在你的答案中包含文字说明。不仅是一个简短评论的代码示例 – awenkhh