1

I have following jQuery Ajax scenario in which a webmethod returns a collection of strings.

  1. The collection can be null
  2. The collection can be non-null but zero records.
  3. 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:

  1. Is object empty?
  2. Javascript's hasOwnProperty() Method Is More Consistent Than The IN Operator

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

        }
        );
 }
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

0

Instead of

if($.isEmptyObject(result))

would

if(typeof result !== 'undefined' && result.length < 1)

work?

Reference for undefined is here JavaScript undefined Property

The undefined property indicates that a variable has not been assigned a value.

daveyfaherty
  • 4,585
  • 2
  • 27
  • 42
0

At present I am using the following. Any improvement suggestions?

 function displayResultForLog(result) 
 {
   if (result.hasOwnProperty("d")) {
       result = result.d
   }

if (result !== undefined && result != null )
{
    if (result.hasOwnProperty('length')) 
    {
        if (result.length >= 1) 
        {
            for (i = 0; i < result.length; i++) {

                var sentDate = result[i];

            }
        }
        else 
        {
            $(requiredTable).append('Length is 0');
        }
    }

    else 
    {
        $(requiredTable).append('Length is not available.');
    }

}
else 
{
    $(requiredTable).append('Result is null.');
}
}

Reference for undefined is here JavaScript undefined Property

The undefined property indicates that a variable has not been assigned a value.

LCJ
  • 22,196
  • 67
  • 260
  • 418