gpt4 book ai didi

jquery - 检查 .getJSON 是否返回任何数据

转载 作者:行者123 更新时间:2023-12-01 00:06:54 24 4
gpt4 key购买 nike

我正在尝试使用 KnockOutJS 和 jQuery - 有什么方法可以检查下面的 allData ,看看它是否返回了任何内容?

如果没有,我想在屏幕上隐藏一个div:

$(document).ready(function () {

$("#thankyou").hide(); // hide thank you box

$("#searchBtn").click(function () {

$.getJSON("/api/searchapi/", function (allData) {
sampleProductCategories = allData; // I want to check if this has returned anything?
if(!allData) { alert("nothing");}
cart.RoomCategories(sampleProductCategories);
});
});
});

Firebug 将空 JSON 显示为:

enter image description here

最佳答案

由于您的“空数据”是一个空数组,因此只需测试它的长度即可。

if (allData.length) {
// we have data
}
else {
// we don't have data
}

关于jquery - 检查 .getJSON 是否返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15745876/

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