json.success does not work or does not enter the code

0

Good someone could tell me why it does not enter my condition if it is only a validation to check if the vin of the car exists if it is not only shows message and if it is if it inserts me it is in MVC, it marks me error in the code only in the controller it marks that the car was found correctly and since at the same time it enters the else of the if of incomplete parameters, I was printing everything but I did not find anything.

mi js

$('.prueba_manejo_tabla').click(function(){
            var id = $('#contacto_prueba_manejo').val();         
            var persona = $('#persona_prueba_manejo').val();
            var fecha = $('#fecha_prueba_manejo').val();
            var ciudad = $('#ciudad').val();
            var inv = $('#idinventario').val();

            if(inv !='' && fecha !='' && persona !=''){
                $.ajax({
                   url: base_url + "buscar/buscar_auto",
                    type: "POST",
                    dataType: "json",
                    data : {contacto : id, inv : inv, persona : persona, fecha : fecha},
                    success: function(json, textStatus, jqXHR){
                        console.log(json);
                        console.log(textStatus);
                        console.log(jqXHR);
                        console.log(inv);
                        if (json.success) {
                            swal('Bien', 'SE ENCONTRO VIN', 'success');
                            $.getJSON(base_url+'contacto/registrar_prueba_manejo_inventario', {contacto : id, inv : inv, persona : persona, fecha : fecha}, function(data)
                            {
                               swal({
                            title: json.titulo,
                            text: json.mensaje,
                            type: json.status,
                            showCancelButton: false,
                            confirmButtonText: 'ok',
                        }).then(function(){
                            $('#modal_prueba_manejo').modal('hide');
                            $('#contacto_prueba_manejo').val('');
                            $('#idcontacto').val('');
                            $('#idinventario').val('');
                            $('#persona_prueba_manejo').val('');
                            if (ciudad == 'Tijuana')
                            var newWindow = window.open("");
                        }); 

                            });
                        }else{
                            swal('Error!', 'NO SE ENCONTRO VIN', 'error');

                        }
                        },
                        complete: function(textStatus, jqXHR){
                    },
                    error: function(textStatus, errorThrown, jqXHR){
                        console.log(textStatus);
                        console.log(errorThrown);
                        console.log(jqXHR);
                    }

                });
            }else{
                swal('Error!', 'Favor de llenar todos los campos', 'error');
            }
        });

CONTROLLER

$respuesta = array ();
        if(isset($_POST['inv']))
        {
            $prueba_manejo = $this->Contactomodel->buscar_auto();
            print_r($prueba_manejo);
            if($prueba_manejo)
            {
                $respuesta['status'] = true;
                $respuesta['titulo'] = 'Listo!';
                $respuesta['mensaje'] = 'Auto encontrado';

            }
            else
            {
                $respuesta['status'] = false;
                $respuesta['titulo'] = 'Error!';
                $respuesta['mensaje'] = 'Error al registrar prueba';
            }
        }
        else
        {
            $respuesta['status'] = 'error';
            $respuesta['titulo'] = 'Error!';
            $respuesta['mensaje'] = 'Parametros incompletos prueba';

        }
        echo json_encode($respuesta);
    
asked by Juan Jose 26.09.2018 в 00:39
source

1 answer

0

Welcome to Stackoverflow.

There are several things:

  • The only thing you should put in the success parameter is the value you expect from the server. Put it like this:

    success: function(json){
    
    ...
    
  • With respect to the if , if you observe in your PHP code, the object that you return does not have a key success , you should verify the key status , which is what would eventually exist in the json:

    if (json.status) {
    
    ...
    
  • To ensure that there will be no coding problems, it is recommended to put a header before printing the result.

    header('Content-type: application/json; charset=utf-8');
    echo json_encode($respuesta);
    
  • With those changes, it should work as expected.

        
    answered by 26.09.2018 / 02:10
    source