Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
How to get message error from Exception whit Ajax MVC 5 core

I'm trying to fill Field with user information using ajax. It work well;

ACTION

  public Operatore User(int Id)
        {
            Operatore User = new Operatore();

            /*
             DO STUFF TO FILL USER

             */

            /*if something go wrong 
              throw new Exception("user does not exist");
             */


            return User;
        }

but if i throw Exception with custom message i can't read it in Ajax

AJAX

     $.ajax({
                        type: "GET",
                        contentType: "application/json; charset=utf-8",
                        url: "@Url.Action("User")",
                        data: { Id: $("#ID").find(":selected").val()},
                        success: function (msg) {
                        /*FILL THE DATA*/
                        /*It works well*/
                        },
                        error: function (error) {
                            alert(error.Message);
                           /*this does not work*/
                        }
                    });

I just need to read my custom error message. The alert return "undefined" How can i do?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If something goes wrong (in operatore):

Response.StatusCode = 500;
Return Json("Error Message");

StatusCode is necessary to mislead Ajax. Beware your action must return an object.

In Ajax:

 error: function (error) {
        alert(error.responseJSON);
 }
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda