Spring, Angular JS and exception handling in the Service layer

1

I have an application that uses version 6.2.0 with version 3.2.14, , 8 and on the front end we use angularjs . The requests are made via ajax .

The application is all set to receive objects in return of the requisitions, since the requests via are all made via Ajax. In certain functionality of the application we have a "common" request using window.location = url , because we need to return a streaming that is nothing more than a byte array to generate a PDF file.

With this, when there is an error in the back end the answer goes to a blank screen with an object

asked by anonymous 04.12.2015 / 13:44

1 answer

2

Obtain the binary content of the file on success. To do this, use the $http service in conjunction with Blob :

$http({
    url: 'site/endPointQueRetornaConteudoBinario',
    method: "POST",
    data: json, //caso você precise mandar algum conteúdo no POST body
    headers: {
       'Content-type': 'application/json'
    },
    responseType: 'arraybuffer'
}).success(function (data, status, headers, config) {
    // O resultado é válido:
    var blob = new Blob([data], {type: "application/pdf"});
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}).error(function (data, status, headers, config) {
    //Falhou: Faça seu handler aqui.
});

Source.

    
04.12.2015 / 14:47