检查.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 }更多推荐
发布评论