I an trying to produce a check to determine if a specific key exists in a json array using jQuery. Specifically, if I see this key, I know that that no objects that I want to display on the page were returned from the server.
The simple check that I'm trying to use looks like this:
if (data.hasOwnProperty('Error - No records found in table')) {
alert('true');
}
The array that is returned from the server looks like this:
[{"Error - No records found in table": ""}]
Full Code:
var url = 'https://blahblah.com';
var postData = $('#BranchSpecials').serialize();
var spinnerBig = $('.loadingSpinner');
var getClearanceItems = $.ajax({
type: 'Post',
url: url,
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: postData,
dataType: 'json',
beforeSend: function(xhr) {
spinnerBig.show();
}
});
getClearanceItems.done(function(data, jqXHR) {
var clearance = $("#clearance");
spinnerBig.hide();
clearance.empty();
if (data.hasOwnProperty('Error - No records found in table')) {
alert('true');
}
var items = [];
$.each(data, function(i, item) {
items.push('<div class="item-block"><a href="' + url + item.ProdLink + '"><img src="https://blahblah.com/Data/' + item.ProdImage + '" width="104" height="104"/></a><div class="item-meta"><p class="desc"><a href="' + url + item.ProdLink + '">' + item.ProductDesc + '</a></p><p class="itemID">Item #: <a href="' + url + item.ProdLink + '" class="uline">' + item.ProductID + '</a></p></div></div>');
});
clearance.append(items.join(''));
clearance.slick({
infinite: true,
slidesToShow: 4,
slidesToScroll: 4,
dots: true,
appendArrows: $('#controls'),
prevArrow: '<button type="button" class="btn btn-default"><i class="material-icons">chevron_left</i></button>',
nextArrow: '<button type="button" class="btn btn-default"><i class="material-icons">chevron_right</i></button>'
});
});
Question: What is wrong with this check?
if (data[0].hasOwnProperty('Error - No records found in table'))