【发布时间】:2022-01-02 00:21:47
【问题描述】:
我正在使用https://disease.sh/v3/covid-19/jhucsse 上的 JSON API。它将数据存储为 JSON 对象列表。我正在制作一个小型网站,允许用户滚动浏览列表,选择他们的国家和省份,并查看该国家/地区的数据。我正在为此使用 JQuery,这是我目前的代码:
const apiLink = "https://disease.sh/v3/covid-19/jhucsse"
jQuery(document).ready(function() {
jQuery("#SubmitButton").click(function() {
country_value = $('#country_picker').val() //Gets the value of the country_value select list in index.html
$.get(apiLink, function(data,status){
console.log(data[0]) // Placeholder code that prints the first JSON object in the list to the console
// Other code here
})
document.getElementById('output').innerHTML = TestHTML; // Fills the 'output' div element with the output of the program
});
});
我将如何解决这个问题?
【问题讨论】:
-
“我使用这个 JSON”、“我有这个代码”、“我该如何解决这个问题?” ...什么问题?
-
而 AJAX 是异步的,你应该在 AJAX 回调中构建 HTML inside。所以基本上将
document.getElementById('output').innerHTML = TestHTML;移入 $.get 函数体。
标签: javascript jquery json list api