在API调用时,判断返回结果是否为空,如果不为空,则将数据添加到数据列表中。
例如,在JavaScript中进行API调用和数据列表更新可以采用以下代码:
fetch('api/data').then(response => response.json())
.then(data => {
if(data.length > 0){
// 将数据添加到数据列表中
data.forEach(item => {
const option = document.createElement('option');
option.value = item.value;
document.getElementById('datalist').appendChild(option);
});
}
});