I have following jQuery Ajax
scenario in which a webmethod returns a collection of strings.
- The collection can be null
- The collection can be non-null but zero records.
- The collection has one or more records
The following code works fine. It uses jQuery.isEmptyObject. It is advised not to use isEmptyObject()
when it is not a Plain Object
.
How can we handle the result without using isEmptyObject() ?
Note: The ajax "result" is coming as "not plain".
Reference:
CODE
//Callback Function
function displayResultForLog(result)
{
if (result.hasOwnProperty("d"))
{
result = result.d
}
if ($.isPlainObject(result)) {
alert('plain object');
}
else
{
alert('not plain');
}
if($.isEmptyObject(result))
{
//Method returned null
$(requiredTable).append('No data found for the search criteria.');
}
else
{
if (result.hasOwnProperty('length'))
{
//Set the values in HTML
for (i = 0; i < result.length; i++)
{
var sentDate = result[i];
}
}
else
{
//Method returned non-null object; but there is no rows in that
$(requiredTable).append('No data found for the search criteria.');
}
}
}
function setReportTable(receivedContext) {
var searchInput = '111';
$.ajax(
{
type: "POST",
url: "ReportList.aspx/GetReportsSentWithinDates",
data: '{"searchInput": "' + searchInput + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
context: receivedContext, //CONTEXT
success: displayResultForLog
}
);
}