|
|
|
|
Strumenti |
23-05-2020, 00:47 | #1 |
Senior Member
Iscritto dal: Apr 2009
Città: Aci Catena (CT)
Messaggi: 1016
|
ASP.net MVC: Exception e chiamata AJAX
Ciao a tutti,
sto realizzando un'applicazione basata su ASP.net MVC. Tramite Ajax effettuo la chiamata ad diversi metodo, esempio può essere: Codice:
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 viene generata l'eccezione, scatta l'error della chiamata ajax, ma non riesco a catturare in alcun modo l'ex.Message: Codice:
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); } Probabilmente sbaglio il tipo di ritorno in caso di exception? Grazie.
__________________
|
23-05-2020, 09:03 | #2 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2745
|
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) |
25-05-2020, 17:32 | #3 |
Senior Member
Iscritto dal: Apr 2009
Città: Aci Catena (CT)
Messaggi: 1016
|
Hai ragione, nella scheda Network del DevTools dovrebbe esserci, ma non trovo nulla...
Non c'è nulla neanche nel tab Response...
__________________
Ultima modifica di robertino_salemi : 25-05-2020 alle 18:41. |
27-05-2020, 07:54 | #4 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2745
|
Però attenzione, lo status code che hai restituito lato server dovrebbe essere 500:
https://developer.mozilla.org/en-US/...TTP/Status/500 mentre il client ha ricevuto 406: https://developer.mozilla.org/en-US/...TTP/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? |
27-05-2020, 14:53 | #5 |
Senior Member
Iscritto dal: Apr 2009
Città: Aci Catena (CT)
Messaggi: 1016
|
Esatto, lo StatusCode è diverso e non mi spiego come mai.
Certo, ecco la chiamata Ajax: Codice:
$.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); } });
__________________
|
03-06-2020, 15:42 | #6 |
Senior Member
Iscritto dal: Apr 2009
Città: Aci Catena (CT)
Messaggi: 1016
|
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 Codice:
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; },
__________________
|
03-06-2020, 19:44 | #7 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2745
|
Ok ottimo! Scusa, volevo provare a riprodurre il problema ma non ho trovato il tempo di farlo.
|
04-06-2020, 12:15 | #8 |
Senior Member
Iscritto dal: Apr 2009
Città: Aci Catena (CT)
Messaggi: 1016
|
Nessun problema!
__________________
|
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:13.