programing

AJAX 응답 데이터가 비어 있는지/빈지/null/정의되지 않음/0인지 확인합니다.

kakaobank 2023. 3. 8. 21:31
반응형

AJAX 응답 데이터가 비어 있는지/빈지/null/정의되지 않음/0인지 확인합니다.

가지고 있는 것:

데이터베이스 조회 후 HTML을 반환하는 jQuery AJAX 함수가 있습니다.쿼리 결과에 따라 함수는 HTML 코드를 반환하거나 원하는 대로 아무것도 반환하지 않습니다(예: 공백).

필요한 것:

데이터가 비어 있는 경우를 조건부로 확인해야 합니다.

내 코드:

$.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!");
    }               
});

문제:

여러 가지 조건을 시험해 봤지만 아무도 데이터를 제대로 확인하지 못했습니다.제 조사 결과에 따르면 빈 경고 메시지는 데이터가

  1. 존재하지 않는
  2. 0과 같다
  3. 길이가 0인
  4. 무효
  5. 정의되어 있지 않다

이 중 하나가 아닌 경우 빈 경고 메시지가 표시되는 데이터를 조건부로 확인하려면 어떻게 해야 합니까?

다음 정답은 Felix Kling에 의해 질문의 코멘트 섹션에 제공되었습니다.

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

이것은 과제문이지 비교문이 아닙니다.또한."undefined"끈이에요, 재산이에요체크는 다음과 같습니다.if (data === undefined)(따옴표 없음, 그렇지 않으면 문자열 값임)

정의되지 않은 경우 빈 문자열을 반환할 수 있습니다.체크해 볼 수도 있어요.falsy와 같은 가치관을 가지다if (!data)뿐만 아니라.

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

이 일은 나에게 효과가 있었다.페이지의 PHP 코드.php

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

그리고 AJAX 코드는

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 );
                              } 
               }); 

메모: 'SQ'라는 단어LSuccess'는 PHP에서 수신해야 합니다.

$.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!");
    }               
});

언급URL : https://stackoverflow.com/questions/23851337/check-if-ajax-response-data-is-empty-blank-null-undefined-0

반응형