Jump to content
PLC Forum


ASP.net MVC: Exception e chiamata AJAX


Recommended Posts

robertosalemi
Posted (edited)

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.

 

Edited by robertosalemi
  • 2 weeks later...

robertosalemi
Posted

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! ;)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...