Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:55:13+00:00 2026-05-26T23:55:13+00:00

I am returning the following object JsonResult return new JsonResult { Data = new

  • 0

I am returning the following object JsonResult

return new JsonResult
            {
                Data = new { ErrorMessage = message },
                ContentEncoding = System.Text.Encoding.UTF8,
                JsonRequestBehavior = JsonRequestBehavior.DenyGet
            };

How do I get the error message out of it on the jquery side?

this is my error delegate of jquery ajax

error: function (result) {
        alert('error');
        alert(result.ErrorMessage);
    }

But it alerts as undefined. I tried result.Data as well as result.message….all undefined.

namespace myApp.ActionFilters
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
    public class AjaxException : ActionFilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.HttpContext.Request.IsAjaxRequest()) return;

            filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext);

            //Let the system know that the exception has been handled
            filterContext.ExceptionHandled = true;
        }

        protected JsonResult AjaxError(string message, ExceptionContext filterContext)
        {
            //If message is null or empty, then fill with generic message
            if (String.IsNullOrEmpty(message))
                message = "Something went wrong while processing your request. Please refresh the page and try again.";

            //Set the response status code to 500
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            //Needed for IIS7.0
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

            return new JsonResult
            {
                Data = new { ErrorMessage = message },
                ContentEncoding = System.Text.Encoding.UTF8,
                JsonRequestBehavior = JsonRequestBehavior.DenyGet
            };
        }
    }
}

In my Controller I have an Action to test this

 [AjaxException]
    public ActionResult TestErrorHandling(string id)
    {
       if (string.IsNullOrEmpty(id))
        {
            throw new Exception("oh no");
        }
        return Json(new { success = true });
     }

In my js

 id = "";
 $.ajax({
    contentType: 'application/json, charset=utf-8',
    type: "POST",
    url: "/Controller/TestErrorHandling",
    data: JSON.stringify({ id: id }),
    cache: false,
    dataType: "json",

    success: function (result) {
        alert('some error occurred: ' + result.ErrorMessage);


        alert('success!');
    },

    error: function (xhr, ajaxOptions, thrownError) {
        alert('error');
        alert(xhr.ErrorMessage);
    }

    });

Question: how do I get the errorMessage inside error delegate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:55:14+00:00Added an answer on May 26, 2026 at 11:55 pm

    How do I get the error message out of it on the jquery side?

    Since you are returning a JSON object that comes with 200 status code the error callback will never be executed. So you could use the success callback in this case:

    success: function(result) {
        if (result.ErrorMessage) {
            alert('some error occurred: ' + result.ErrorMessage);
        }
    }
    

    Or if you want the error handler to be executed make sure that you set the proper status code in your controller action:

    public ActionResult Foo()
    {
        Response.StatusCode = 500;
        return Json(new { ErrorMessage = message });
    }
    

    and then:

    error: function (jqXHR) {
        var result = $.parseJSON(jqXHR.responseText);
        alert('some error occurred: ' + result.ErrorMessage);
    }
    

    You might also find the following answer useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: [HttpPost] [AjaxOnly] public JsonResult ValidateInput(string text) { return new
I'm calling a web service and returning the following data in JSON format: [{OrderNumber:12345,CustomerId:555}]
I have the following code (calling a method returning IEnumerable): FibonacciWithLinq.Skip(delegate() { return 5;
We are returning the following JSON from a ruby code: { label:CPU-7, data:[[50,45,38,34]], settings:{
I have the following code returning the error 'object' does not contain a constructor
[myDictionary objectForKey:kYourKey]; is returning NSString Object, but following code: if (![[myDictionary objectForKey:kYourKey] isEqualToString:@<null>]) {
I have a WCF service, hosted in IIS returning the following error (when trying
In writing a django app, I am returning the following json on a jQuery
Given the following: #light //any function returning bool * 'a let foo = let
Consider the following code where I am returning double& and a string& . It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.