Capture server response through ajax

0

Good day, I have an ajax that goes and consults something and according to the query returns me in an echo $ tipo_de_solicitud; a variable which I know is doing well because I check it by the chrome inspector (F12), the question is to receive it in the ajax and want to do something depending on what has been answered there in the .php

here the ajax

function confirmar_visita2(boton2)
{

  let id = boton2.value;

  var confirmar_visita = confirm("Confirmar visita para solicitud #"+id);

  if (confirmar_visita == true)
  {
    //recogo valores fecha hora y tecnico asignado
    var fecha=$('#input_fecha_visita').val();
    var hora=$('#input_hora_visita').val();

    var selector = document.getElementById("select_tecnico_para_visita");
    var tecnico_escogido = selector.options[selector.selectedIndex].text;

    var data = new FormData();

    data.append('id',id);
    data.append('fecha',fecha);
    data.append('hora',hora);
    data.append('tecnico_escogido',tecnico_escogido);

    //envio al php

    var url = '../PHP/confirmar_visita_solicitud_revision_tecnica.php';
    $.ajax
    ({
      url:url,
      type:'POST',
      contentType:false,
      data:data,
      processData:false,
      cache:false,
      beforeSend: function(resp)
      {
        $("#span_estado").html("cargando...");
      },
      success: function()
      {
        //aqui el problema, he intentado sin el .trim, me dice exactamente que no pue
        var respuesta=this.responseText();

// aqui hago algo dependiendo de la respuesta, como he dicho eso funciona bien en el php
        if(respuesta=='revision_tecnica')
        {
          consultar_solicitudes_revision_tecnica();
        }

        if(respuesta=='instalacion')
        {
          consultar_solicitudes_revision_tecnica();
        }
        //borro campos por si se necesita despues para confirmar otra solicitud
        $('#input_hora_visita').val('');
        $('#input_fecha_visita').val('');
      }
    });
  }
  if (confirmar_visita == false)
  {
    return;
  }
}
    
asked by Gabriel Uribe Gomez 16.06.2018 в 02:08
source

1 answer

0

The success function receives a response parameter, try this:

$.ajax
    ({
      url:url,
      type:'POST',
      contentType:false,
      data:data,
      processData:false,
      cache:false,
      beforeSend: function(resp)
      {
        $("#span_estado").html("cargando...");
      },
      success: function(respuesta)
      {                
// aqui hago algo dependiendo de la respuesta, como he dicho eso funciona bien en el php
        if(respuesta=='revision_tecnica')
        {
          consultar_solicitudes_revision_tecnica();
        }

        if(respuesta=='instalacion')
        {
          consultar_solicitudes_revision_tecnica();
        }
        //borro campos por si se necesita despues para confirmar otra solicitud
        $('#input_hora_visita').val('');
        $('#input_fecha_visita').val('');
      }
    });
    
answered by 16.06.2018 / 02:10
source