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

167
Views
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 answers
Answer question

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