Treat Exception on return from Ajax jsonp

Asked

Viewed 160 times

0

Personally I’m making the service call this way:

$.ajax({
    url: minhaurl,
    type: "GET",
    dataType: "jsonp",
    contentType: "application/json; charset=utf-8",
    async: false,
    success: function (result, status, request) {
        console.log('sucesso');
    },
    error: function (request, status, erro) {
        console.log('sucesso');
    }
});

When I receive service success the expected return is json equal to this:

callback({"d":{"Mensagem":"","Id":"1000","DDD":"0011","Telefone":"999999999","Fax":"999999999","Flag1":true,"Segmento":"A","Horario_diferenciado":null,"Caixa_exclusivo":"1","CaixaEletronico":null,"Nome":"Nome da agencia","Endereco":"Rua A","Cep":"09050-380","Uf":"SP","Cidade":"SAO PAULO","Bairro":"PQ BANDEIRANTE","Abertura":"10h:00","Fechamento":"16h:00","Longitude":-46.641869,"Latitude":-23.634659,"Imagem":"icone.png"}});

When I get service error the return is this:

callback({"Message":"Object reference not set to an instance of an object.","StackTrace":"   at Banco.WS.BuscadorAgencias.BuscaAgenciaPorNumero()","ExceptionType":"System.NullReferenceException"});

The problem is that even writing Success and error, when the error occurs I cannot treat it.

In the browser console the error appears:

jquery-1.11.3.js:9836 GET minhaurl&callback=jQuery111306013578134038797_1477314780144&_=1477314780145

1 answer

0

I was able to solve this problem by putting a timeout on the ajax call. Unfortunately I could not otherwise.

Apparently when my service returns an Exception jquery is lost and cannot unmount callback data.

Putting the timeout in ajax function I managed to fall in the catch and display my message.

Browser other questions tagged

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