检查.getJSON是否返回任何数据(Check if .getJSON returned any data)

编程入门 行业动态 更新时间:2024-10-12 05:54:27
检查.getJSON是否返回任何数据(Check if .getJSON returned any data)

我正在尝试使用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显示为:

I'm trying to use KnockOutJS and jQuery - is there any way I can check allData below, to see if it has returned anything?

If it hasn't, I want to hide a div on the screen:

$(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 shows the empty JSON as:

最满意答案

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

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

Since your "empty data" is an empty array, just test it's length.

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

更多推荐

本文发布于:2023-08-04 00:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   getJSON   Check   data   returned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!