Invoke ajax error

2

I'm using, to show you're loading while shipping the client to the web service via php

//Tipo do método
            type: "POST",
            url: "../Processos/AcCadasCliente.php",
            data: $('#formulario').serialize(), 
                beforeSend: function () {
                    // enquanto a função esta sendo processada,
                    // a mensagem "Carregando"
                    $('#enviar').html('.....');
                },
                //Quando terminado a função
                success: function (txt) {
                    //Se deu certo, mostrar mensagem, enviado com sucesso
                    $('#enviar').html('.....');
                },
                //Em caso de erro
                error: function (txt) {
                  $('#enviar').html('Erro');
                }

It happens that even without having a connection to the web service, still, it only executes beforesend and sucess. How to invoke the ajax error

    
asked by anonymous 31.07.2015 / 22:14

1 answer

0

Ideally you should manage the request status with php, you can use this:

if ($tudo_ok)
    {
        header('Content-Type: application/json');
        print json_encode($result);
    }
else
    {
        header('HTTP/1.1 500 Internal Server');
        header('Content-Type: application/json; charset=UTF-8');
        die(json_encode(array('message' => 'ERROR', 'code' => 1337)));
    }
    
31.07.2015 / 22:43