Check if AJAX response data is empty/blank/null/undefined/0
ajax, javascript, jquery, null
Solution
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);
}
Problem
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 - empty - non-existent - equal to zero - of length zero - null - undefined If it's none of these things, how can I therefore conditionally check for the data that yields a blank alert message?