Handle exception on Ajax jsonp return

0

Personally I'm calling the service 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 get 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 can not handle it.

The browser console displays the error:

  

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

    
asked by anonymous 24.10.2016 / 15:19

1 answer

0

I was able to solve this problem by placing a timeout on the ajax call. Unfortunately I could not do it any other way.

Apparently when my service returns an exception the jquery gets lost and can not unmount the callback data.

Putting the timeout in the ajax function I was able to drop into catch and display my message.

    
24.10.2016 / 18:27