Return "Undefined" Ajax PHP

Asked

Viewed 218 times

1

Next I’m trying to return a database value via Ajax.

By clicking the button:

<button id="btn-database-finder" name="database-link" type="button" class="btn btn-outline btn-primary btn-xs btn-to-right" data-toggle="modal" data-target="#databaseModal">Visualizar</button>

He’s going to Ajax:

$("#btn-database-finder").on("click",function(){
    var radioid = $('#id-viewer').html();   
    $.ajax({
        type:"post",
        url:"../connect/post/select.php",
        data:'database-link='+radioid,
        success:function(responseData, textStatus, jqXHR, data){
            $("#database-viewer").html('<a href=../dashboard/dist/archives/databases/'+data+'><i  class="fa fa-file-archive-o fa-5x" aria-hidden="true" style="cursor: pointer;"></i></a>'); 

         }      
      });

});

That takes this line:

<i class="fa fa-hashtag" id="id-viewer" aria-hidden="true"></i>

Where is the ID join the TAG li.

This part is all working, putting a Alert() appears the ID, but when he goes to the url:"../connect/post/select.php",

That is:

if($_SERVER['REQUEST_METHOD']=='POST'){ 


       $stDatabase = $_POST['database-link'];           
       $sendDatabase = $selDatabase->selectDatabase($stDatabase);


        exit();
}   

It always returns the as Undefined:

<a href="dashboard/dist/archives/databases/undefined"><i class="fa fa-file-archive-o fa-5x" aria-hidden="true" style="cursor: pointer;"></i></a>
  • According to the api, there are only 3 parameters in success: data, textStatus and jqXHR.

1 answer

1


Javascript does not receive anything because it is missing "print" the answer in the PHP output. I put an example to improve, sending headers to identify the response as JSON:

if($_SERVER['REQUEST_METHOD']=='POST'){ 


       $stDatabase = $_POST['database-link'];           
       $sendDatabase = $selDatabase->selectDatabase($stDatabase);

       // Envia headers com content-type adequado
       header( 'Content-Type: application/json; charset=UTF-8' );
       // Imprime o resultado como se fosse mostrar na tela
       echo json_encode($sendDatabase);
       exit();
}
  • Now he’s coming back as object.

  • 1

    @Wagnervian in the javascript response, before the $("#database-viewer").html, place console.log(responseData) and glue here what appears on the console.

  • Opa ruined a thing and now appears Object {Radiodatabase: "581b122242703.rar"}

  • 1

    This seems closer to the result. Swap /'+data+' for /'+responseData.RadioDatabase+' in the javascript response and should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.