Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Cómo obtener un mensaje de error de excepción con Ajax MVC 5 core

Estoy tratando de llenar Field con información de usuario usando ajax. Funciona bien;

ACCIÓN

 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; }

pero si lanzo una excepción con un mensaje personalizado, no puedo leerlo en 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*/ } });

Solo necesito leer mi mensaje de error personalizado. La alerta devuelve "indefinido" ¿Cómo puedo hacer?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si algo sale mal (en operatore):

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

StatusCode es necesario para engañar a Ajax. Cuidado, tu acción debe devolver un objeto.

En el Ajax:

 error: function (error) { alert(error.responseJSON); }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!