Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda