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

前端 未结 5 733
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 23:57

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 co

相关标签:
5条回答
  • 2020-12-23 00:04

    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);
    }
    
    0 讨论(0)
  • 2020-12-23 00:04
    if(data.trim()==''){alert("Nothing Found");}
    
    0 讨论(0)
  • 2020-12-23 00:06
    $.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!");
        }               
    });
    
    0 讨论(0)
  • 2020-12-23 00:08

    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

    0 讨论(0)
  • 2020-12-23 00:27
    //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

    0 讨论(0)
提交回复
热议问题