How to display a query variable in ajax

1

I want to show in my teacher variable data in the value the teacher's id, and in between the tags Name and surname I let you capture my console and my code. - > male teacher = '';

What's in my Console

My java script code

$(function() {
$( '#select_profesor' ).on('change',onprofesorchange);

});

function  onprofesorchange () {

var id= $(this).val();
  $.get('/api/asignar-profesor/'+id+'/profesor', function(datos)  {
console.log(datos);

 var profesor='<span  class="btn btn-success btn-xs" value="">  </span>';

$('#div1').html(profesor);



});
    
asked by Jcastillovnz 07.11.2017 в 01:09
source

1 answer

0

Since the answer is an array you only have to access the index 0 and then the id and surname properties:

$(function() {
    $('#select_profesor' ).on('change',onprofesorchange);
});

function  onprofesorchange () {
    var id= $(this).val();
    $.get('/api/asignar-profesor/'+id+'/profesor', function(datos)  {
        console.log(datos);

        // accedemos al indice 0 y lo mostramos
        var profesor='<span  class="btn btn-success btn-xs" value="'+datos[0].id+'">' + datos[0].nombre + ' ' + datos[0].apellido +' </span>';
        $('#div1').html(profesor);
    });
}
    
answered by 07.11.2017 в 13:42