Invoke ajax error

Asked

Viewed 127 times

2

I am using, to show that you are loading while sending 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 I have connection with the web service, still, only runs the beforesend and sucess. How to invoke ajax error

  • Are you sure the web service is not accessible? If there is no connection it should be an error. Do you think you can reproduce the error here -> http://jsfiddle.net/oxu8ux6m/?

  • I have yes, in php code it enters the Cath, but gets only in this, it does not go to ajax error

  • no código php ele entra no catch So he is able to connect with the server, IE, the request is not having problems and the function success will always be called.

  • There is no error no request, but there is error in the answer because it is not connected with web service, and it was this error that I wanted to call the javascript error

1 answer

0

Ideally you manage the request status with php, you can use it like 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)));
    }
  • Nothing appears on the screen.

  • You must not have understood what he meant. Just copying and pasting the answer won’t work. In php you have to handle the error connected with the webservice and print something so that the javascript Success function will handle the error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.