PDA

View Full Version : ASP.net MVC: Exception e chiamata AJAX


robertino_salemi
23-05-2020, 00:47
Ciao a tutti,
sto realizzando un'applicazione basata su ASP.net MVC.

Tramite Ajax effettuo la chiamata ad diversi metodo, esempio può essere:

public ActionResult MyMethod(DateTime ref_date) {
try
{
//code

return new FileContentResult(stream.ToArray(), "application/pdf");
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
}
}


Se l'eccezione non scatta, nella success della chiamata ajax eseguo la procedura desiderata...

Se viene generata l'eccezione, scatta l'error della chiamata ajax, ma non riesco a catturare in alcun modo l'ex.Message:

error: function (err, type, httpStatus) {
console.log(err);
var failureMessage = 'Error occurred in ajax call ' + err.status + " - " + err.responseText + " - " + httpStatus;
console.log(failureMessage);
console.log(err.responseText);
}


Se ad esempio il metodo va in eccezione perchè il file usato come modello è lockato da un altro processo, nell'eccezione del metodo lo vedo, nella chiamata Ajax no, non riesco a catturarlo.

Probabilmente sbaglio il tipo di ritorno in caso di exception?

Grazie.

wingman87
23-05-2020, 09:03
Ciao,
ora non ho modo di provare direttamente, io procederei così:

1. Verifica dai developer tools del browser (nella scheda Net) che il messaggio dell'eccezione arrivi al client (dovrebbe essere nel body o in un qualche header della risposta).

2a. Se il messaggio arriva allora devi fare qualche correzione nel javascript. Non ho capito che metodo stai utilizzando per effettuare la richiesta (jQuery ajax?), guarderei la documentazione per capire dove dovresti trovare il body o gli header della risposta

2b. Se il messaggio non arriva allora devi fare una correzione lato server, forse il metodo che stai utilizzando non restituisce il messaggio al client (anche se mi sembra strano)

robertino_salemi
25-05-2020, 17:32
Hai ragione, nella scheda Network del DevTools dovrebbe esserci, ma non trovo nulla...

https://i.ibb.co/YhLFZXH/capture-1.png (https://ibb.co/qpr35CT)

https://i.ibb.co/1Qtt75L/capture-2.png (https://ibb.co/V9ccJZt)

Non c'è nulla neanche nel tab Response...

wingman87
27-05-2020, 07:54
Però attenzione, lo status code che hai restituito lato server dovrebbe essere 500:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500

mentre il client ha ricevuto 406:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406

Da quello che leggo potrebbe essere dovuto al fatto che nella Request è specificato Accept:application/pdf (non si vede nel tuo screenshot ma provo a supporlo, dovrebbe essere nei Request Headers) e lato server in caso di errore non restituisci un application/pdf.
Come hai effettuato la richiesta ajax? Puoi postare l'intero snippet javascript?

robertino_salemi
27-05-2020, 14:53
Esatto, lo StatusCode è diverso e non mi spiego come mai.

Certo, ecco la chiamata Ajax:

$.ajax({
type: "POST",
url: url_with_params,
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem(JWTTokenName),
'Content-Type': "application/json;charset=UTF-8"
},
// Seems like the only way to get access to the xhr object
xhr: function () {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob'
return xhr;
},
data: jsonData,
cache: true,
success: function (data, status, xhr) {
var blob = new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });

var downloadUrl = URL.createObjectURL(blob);

//Get fileName
var headers = xhr.getAllResponseHeaders();
var arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
var headerMap = {};
arr.forEach(function (line) {
var parts = line.split(': ');
var header = parts.shift();
var value = parts.join(': ');
headerMap[header] = value;
});
var fileName = headerMap["content-disposition"];

var a = document.createElement("a");
a.href = downloadUrl;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
error: function (err, type, httpStatus) {
var failureMessage = 'Error occurred in ajax call ' + err.status + " - " + err.responseText + " - " + httpStatus;
console.log(failureMessage);
}
});

robertino_salemi
03-06-2020, 15:42
Ho trovato l'errore, modificando questa parte della chiamata ajax:

- se la risposta è success allora il type atteso è un blob
- se la risposta è error allora il type atteso è un text


xhr: function () {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 2) {
if (xhr.status == 200) {
xhr.responseType = "blob";
} else {
xhr.responseType = "text";
}
}
};
return xhr;
},


Grazie! ;)

wingman87
03-06-2020, 19:44
Ok ottimo! Scusa, volevo provare a riprodurre il problema ma non ho trovato il tempo di farlo.

robertino_salemi
04-06-2020, 12:15
Nessun problema! :)