Return "Undefined" Ajax PHP

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>

It goes 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>'); 

         }      
      });

});

What's in the box:

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

Where is the ID within the TAG li .

In this part everything is working, putting alert () appears ID , but when it goes to url:"../connect/post/select.php",

What 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>
    
asked by anonymous 03.11.2016 / 15:01

1 answer

1

JavaScript does not receive anything because it's missing the "print" response in the PHP output. I put an example to improve by 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();
}
    
03.11.2016 / 16:58