我无法访问并将AJAX调用的响应附加到事实API。
我收到了未定义的错误,但是我尝试构造代码访问并迭代响应。
我已成功将数据记录到控制台,现在只需要在页面上添加HTML。
以下是当前的代码和API响应结构,我不明白的是何时使用data
以及它与此有何关系?这是从请求或特定API结构接收的任何数据的关键字。
CODE:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
console.log('dom ready');
$("#search").on("click", runTing);
function runTing () {
var url = "http://api.v3.factual.com/t/places?q=Aldi,London&filters={%22country%22:%22GB%22}&KEY=111111111111111111111111";
$.ajax({
url: url,
dataType: "JSON",
success: function (data) {
var $latitude = $("<p>").text(response.data[0].address);
$('#info').append("$latitude");
}
});
};
});
</script>
</head>
<body>
<div id="info"></div>
</body>
JSON响应:
{
"version":3,
"status":"ok",
"response":{
"data":[
{
"address":"632-640 Kingsbury Rd",
"admin_region":"England",
"category_ids":[
171
],
"category_labels":[
[
"Retail",
"Supermarkets and Groceries"
]
],
"country":"gb",
"factual_id":"75fda75e-41a7-4645-b47a-9af5364fead1",
"hours":{
"monday":[
[
"8:00",
"21:00"
]
],
"tuesday":[
[
"8:00",
"21:00"
]
],
"wednesday":[
[
"8:00",
"21:00"
]
],
"thursday":[
[
"8:00",
"21:00"
]
],
"friday":[
[
"8:00",
"21:00"
]
],
"saturday":[
[
"8:00",
"21:00"
]
],
"sunday":[
[
"10:00",
"16:00"
]
]
},
"hours_display":"Mon-Sat 8:00 AM-9:00 PM; Sun 10:00 AM-4:00 PM",
"latitude":51.584985,
"locality":"London",
"longitude":-0.279941,
"name":"Aldi",
"neighborhood":[
"Kingsbury",
"Queensbury"
],
"post_town":"London",
"postcode":"NW9 9HN",
"region":"Greater London",
"tel":"0844 406 8800",
"website":"http://www.aldi.co.uk/"
},
{
"address":"1-4 London Rd",
"admin_region":"England",
"category_ids":[
171
],
"category_labels":[
[
"Retail",
"Supermarkets and Groceries"
]
],
"country":"gb",
"factual_id":"7edfabf8-3f28-4ee4-9322-6a296ed09a59",
"hours":{
"monday":[
[
"8:00",
"20:00"
]
],
"tuesday":[
[
"8:00",
"20:00"
]
],
"wednesday":[
[
"8:00",
"20:00"
]
],
"thursday":[
[
"8:00",
"20:00"
]
],
"friday":[
[
"8:00",
"20:00"
]
],
"saturday":[
[
"8:00",
"20:00"
]
],
"sunday":[
[
"10:00",
"16:00"
]
]
},
"hours_display":"Mon-Sat 8:00 AM-8:00 PM; Sun 10:00 AM-4:00 PM",
"latitude":50.829975,
"locality":"Brighton",
"longitude":-0.136322,
"name":"Aldi",
"neighborhood":[
"North Laines"
],
"post_town":"Brighton",
"postcode":"BN1 4JA",
"region":"East Sussex",
"tel":"0844 406 8800",
"website":"http://www.aldi.co.uk/"
},
答案 0 :(得分:6)
response.data[0].address
应该是
data.response.data[0].address
从BE返回的对象当前位于data
(回调参数)中。
因此,在尝试访问响应属性之前,还有一个嵌套层。
同样@chaarlietfl指出
$('#info').append("$latitude");
^ ^
---------------> Need to get rid of quotes