Check if AJAX response data is empty/blank/null/undefined/0

238,721

Solution 1

The following correct answer was provided in the comment section of the question by Felix Kling:

if (!$.trim(data)){   
    alert("What follows is blank: " + data);
}
else{   
    alert("What follows is not blank: " + data);
}

Solution 2

//if(data="undefined"){

This is an assignment statement, not a comparison. Also, "undefined" is a string, it's a property. Checking it is like this: if (data === undefined) (no quotes, otherwise it's a string value)

If it's not defined, you may be returning an empty string. You could try checking for a falsy value like if (!data) as well

Solution 3

if(data.trim()==''){alert("Nothing Found");}

Solution 4

This worked form me.. PHP Code on page.php

 $query_de="sql statements here";
 $sql_de = sqlsrv_query($conn,$query_de);
      if ($sql_de)
      {
        echo "SQLSuccess";
       }
         exit();

and then AJAX Code has bellow

jQuery.ajax({
                    url  : "page.php",
                    type : "POST",
                    data : {
                            buttonsave   : 1,
                            var1         : val1,
                            var2         : val2,
                              },
                  success:function(data)
                           {
                     if(jQuery.trim(data) === "SQLSuccess")
                                   {
                           alert("Se agrego correctamente");
                          // alert(data);
                                      } else { alert(data);}
                              },
                   error: function(error)
                           {
                    alert("Error AJAX not working: "+ error );
                              } 
               }); 

NOTE: the word 'SQLSuccess' must be received from PHP

Solution 5

$.ajax({
    type:"POST",
    url: "<?php echo admin_url('admin-ajax.php'); ?>",
    data: associated_buildsorprojects_form,
    success:function(data){
        // do console.log(data);
        console.log(data);
        // you'll find that what exactly inside data 
        // I do not prefer alter(data); now because, it does not 
        // completes requirement all the time 
        // After that you can easily put if condition that you do not want like
        // if(data != '')
        // if(data == null)
        // or whatever you want 
    },
    error: function(errorThrown){
        alert(errorThrown);
        alert("There is an error with AJAX!");
    }               
});
Share:
238,721
Clarus Dignus
Author by

Clarus Dignus

Updated on July 08, 2022

Comments

  • Clarus Dignus
    Clarus Dignus almost 2 years

    What I have:

    I have jQuery AJAX function that returns HTML after querying a database. Depending on the result of the query, the function will either return HTML code or nothing (i.e. blank) as desired.

    What I need:

    I need to conditionally check for when the data is blank.

    My code:

    $.ajax({
        type:"POST",
        url: "<?php echo admin_url('admin-ajax.php'); ?>",
        data: associated_buildsorprojects_form,
        success:function(data){
            if(!data){  
            //if(data="undefined"){
            //if(data==="undefined"){
            //if(data==null){
            //if(data.length == 0){
            //if ( data.length != 0 ){
            //if(data===0){
            //if(data==="0"){   
                alert("Data: " + data);
            }
        },
        error: function(errorThrown){
            alert(errorThrown);
            alert("There is an error with AJAX!");
        }               
    });
    

    My problem:

    I've tried a variety of conditions but none correctly check the data. Based on my findings, a blank alert message does not mean data is

    1. empty
    2. non-existent
    3. equal to zero
    4. of length zero
    5. null
    6. undefined

    If it's none of these things, how can I therefore conditionally check for the data that yields a blank alert message?

  • Clarus Dignus
    Clarus Dignus almost 10 years
    if (data == undefined) works. Thank you for the explanations. Selected as correct answer chronologically.
  • Rory McCrossan
    Rory McCrossan over 8 years
    Code is great, but some explanation of the problem and how this solves it would really help.
  • blackandorangecat
    blackandorangecat over 6 years
    I suggest using console.log(); rather than alert();. It is better practice. If you get in the habit of alerting your error messages you may one day find yourself in an infinite loop of alert boxes and no ability to close the browser/program. That being said, nice answer.
  • zeroquaranta
    zeroquaranta over 6 years
    what if my data is an array and I wanted to treat separately the case of an empty array?
  • The Vojtisek
    The Vojtisek over 5 years
    @tHomas then you just add the condition for the case of an empty array
  • Brettins
    Brettins almost 5 years
    data != '' can potentially fire on empty ajax return, so the comment suggestion is incorrect. The trim() function suggested by almost every other post is necessary.
  • Colin 't Hart
    Colin 't Hart about 4 years
    This doesn't answer the question. The asker explicitly wants to know how to test for a non-response in the body -- not send some string instead.