jQuery to check if returned data object is empty

$("#post").live("click",function()
{
    $("input:checkbox[name='bookmarkid']:checked").each(function()
    {
         $.post("php/socialbookmark-post.php", {bookmarkID: $(this).val()},function(data)
         {
              if(data != "") alert(data);
         });
    });
});

the php file outputs some text only if something goes wrong. The checking that I do fails if the data is empty and displays an empty message. I need to fix this. Any ideas?

Answers


You could instead check if data exists, like this:

if(data && data !="") alert(data);

data will be null in your case, and null != "", so the if is passing.


I also face the same issue. The data block will not execute of if data not returned from ajax. But find a way to solved by referring jQuery Ajax documents:

http://api.jquery.com/jQuery.ajax/

By the implementation we have used the sucess method of done and if data not returned it will not executed. Instead fail method will get called. Refer to

fail(function())

If data is empty then condition for check data !== '' it will generate false

if(data !== 'undefined' && data !== ''){
console.log('data is not empty')
} else {
console.log('data is empty')
}

Need Your Help

Really big number

c++ biginteger

First of all apologies if there is already a topic like this but I have not found... I need to know how to handle a really big number such as the result of 789^2346:

delete photos in my project

ios objective-c photolibrary

I understand that I can't programmatically delete photos from my app . Because Apple writes that we can only add photos