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

240
Views
jQuery Ajax error show Exception Response Message

I know this is a duplicate question but none of the others is helping me on this:

On the following Ajax:

$.ajax({
           type: "POST",
           url: "url",
            data: {
                     //data
                   },
            dataType: "json",
            success: function (data) {
                     //do success logic
                    },
             error: function (req, status, error) {
                     var err = req.responseText.Message;
                     console.log(err);
                     }
        });

I need to display only the exception message returned from the server:

which is only: "Error al procesar el recorrido o el mismo es inexistente"

but the req.responseText is the following:

responseText: "System.Exception: Error al procesar el recorrido o el mismo es inexistente\r\n   at 
 etc etc etc\r\n"

I already tried some approaches like: response = jQuery.parseJSON( req.responseText ); but this fails on parsing

or:

req.responseJSON.Message 

but shows undefined

I'm not being able to show only the custom exception message, any ideas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try to use req.responseJSON.value, not req.responseJSON.Message. It works fine in my side.

enter image description here


My test code

Javascript code

function ShowException() {
    $.ajax({
        type: "POST",
        url: "/Home/ShowException",
        data: {
            type:1
        },
        dataType: "json",
        success: function (data) {
            alert(data.value);
        },
        error: function (req, status, error) {
            var err = req.responseJSON.value;
            console.log(err);
        }
    });
}

C# code

[HttpPost]
public IActionResult ShowException(int type)
{
    try
    {
        if (type == 1)
        {
            return Ok(Json("success"));
        }
        else {
            return NotFound(Json("404 not found"));
        }
        
    }
    catch (Exception ex)
    {
        return BadRequest(Json("456"));
        throw;
    }
}
over 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!