gpt4 book ai didi

javascript - 在 Javascript 中使用 Ajax 检索 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 00:02:44 25 4
gpt4 key购买 nike

我不认为这是重复,因为我在任何地方都找不到具体的答案。我是 JavaScript 新手,不知道如何使用 Ajax 提取 JSON 信息并将其显示在我的页面上。我知道我做错了什么,因为我不太理解这些语言。

我的 JSON 如下所示:

{ "lots" : [
{
"name" : "NE Corner HW4 & 41st",
"info" : "$2/Hr<br>Monthly Parking Available",
"center" : {
"lat" : 57.659390,
"lng" : -127.414754
},
"topLeft" : {
"lat" : 57.659616,
"lng" : -127.415102
},
"bottomRight" : {
"lat" :57.659208,
"lng" :-127.414371
}
}, ...etc
]}

这是 Ajax 调用(这很可能是错误的):

var jsonFile = $.ajax({
method: "GET",
url: "filepath/filename.json",
dataType: "JSON"
});

然后我需要在几个函数中使用该信息:

for (var i = 0; i < jsonFile.lots.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(jsonFile.lots[i].center.lat, jsonFile.lots[i].center.lng)
marker.setMap(map);
}

我做错了什么(除了一切)?是我的 JSON 数组吗?我需要解析或字符串化它吗?也许是 Ajax 调用。 JavaScript?我觉得这可能就是一切。感谢您的帮助!

最佳答案

您必须成功才能处理收到的响应。示例

$.ajax({
url: 'Your service url',
type: 'GET' or 'POST',
dataType: 'json',
success: function(response){
//Do what ever you want with the response variable
//Do a console.log(response) to see what is coming as the response
}
})

根据您的示例,您可以使用以下内容。

jsonFile.done(function(response){
//Do what ever your want with the response.
})

关于javascript - 在 Javascript 中使用 Ajax 检索 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com