InGeek
InGeek

Reputation: 2682

ajax on empty result, JQuery

what should I use if i have an empty result coming from C# method.

jQuery.ajax({
        type: "POST",
        url: "Me.aspx/Load",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

            success: function (msg) {

                    alert(msg.d);
                }

});

is not alerting when is empty.

I have tried:

msg.length===0

and

error:

but couldn't make it work.

thank you.

Upvotes: 0

Views: 460

Answers (1)

bgusach
bgusach

Reputation: 15163

If the result is an empty string, then you should check for an empty string:

msg === ''

Sorry, even better:

if (msg) {...}

If msg is anything that is not :

  • false
  • 0
  • null
  • '' (empty string)
  • NaN
  • undefined

it evaluates to true.

What are you expecting from the server?

EDIT: return an object with a success property, something like this:

{
    "success": true,
    ...
}

and then check for

if (msg.success) { ... do stuff ...}

REEDIT

I don't know how it works in C#, but you have to serialize the object you are working with in the server. Let's say this is your (failure) object:

response // and response.success = false

So, you should do something like:

print json_encode(response);

So that it is returned to your JS programme.

Upvotes: 3

Related Questions