Retrieving error message in ASP.NET Ajax upon failure

Using MVC2, I am attempting to show a user-friendly error message. Here is my approach:

public ActionResult Foo()
{
  Response.StatusCode = 403;

  return new ContentResult() { Content="friendly error" }
}

However, I am struggling to figure out how to access it in the AJAX OnFailure callback:

function ajaxErrorHandler(context) {

// This triggers an error, stating that it's not a JSON object
    alert(context.get_response().get_object());

}

I can confirm through Firebug that the response is correct. Any suggestions?

Answer №1

Below is the provided code snippet:

alert(context.get_data());

The above code will show a "friendly error" message.

Answer №2

Here is a suggestion to consider: To access the message within the onFailure callback, you can use context.get_message()...

In your code snippet, make sure to include the following function for handling failures:

function OnFailure(context) {
            alert(context.get_message());
    }

I found this approach to be effective. -- Sudipto www.supernovaservices.com

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the href attribute of an anchor tag that does not have a specified class or

I am currently using an event plugin that automatically links all schedule text to the main event page through anchor tags, like this: <td><a href="http://apavtcongresso.staging.wpengine.com/event/congresso-apavt-2018/">Chegada dos primeiros c ...

Is it possible for PHP to delay its response to an ajax request for an extended period of time?

Creating a chat website where JavaScript communicates with PHP via AJAX, and the PHP waits for the database to update based on user input before responding back sounds like an intriguing project. By using a recall function in AJAX, users can communicate ...

Is there a way to make this AngularJS service wait until it has a value before returning it?

Multiple controllers call a service that loads data into an object named categories: .service('DataService', ['$http', '$q', function($http, $q) { var categories = {}; // Public API. return({ setCategory ...

JavaScript Accordion malfunctioning

I'm attempting to implement an accordion feature for each row of an HTML table using the script provided below HTML <table class="list" id="table" data-bind="visible: !loading()"> @*<table class="productTable" data-bind="sortTable:true" ...

Executing asynchronous functions that invoke other asynchronous functions

I'm facing an issue with executing a sequence of asynchronous functions inside an array. When I call the functions individually, it works perfectly as shown in the example below: function executeAll(string) { return new Promise(function (resolv ...

Stopping the carousel animation in Bootstrap 5: A step-by-step guide

I'm struggling to stop a Bootstrap 5 carousel with a button click. The pause function seems to be ineffective. You can view the code on Code Pen Example or see the code below. document.getElementById("btnPause").addEventListener("clic ...

Complete my search input by utilizing ajax

It's only been 30 minutes since my last post, but I feel like I'm making progress with my search posts input: I've developed a model that resembles this: function matchPosts($keyword) { $this->db->get('posts'); ...

jQuery fails to register a click event on a child element

I've designed a div element to serve as a contact list, containing other divs within it. I'm attempting to set up a click handler on each list item (inner div) but the click event is not being triggered. $(".contact").on("click", function() { ...

Registering components globally in Vue using the <script> tag allows for seamless integration

I'm currently diving into Vue and am interested in incorporating vue-tabs into my project. The guidelines for globally "installing" it are as follows: //in your app.js or a similar file // import Vue from 'vue'; // Already available imp ...

The call to the hook is invalid. In order to use hooks, they must be called within the body of a function component in

So, in my upcoming application, I am incorporating the react-google-one-tap-login library which features the useGoogleOneTapLogin hooks that need to be invoked within a React component. However, when I attempt to use it in this manner: func() { useGoogle ...

Utilize JavaScript to submit the FORM and initiate the 'submit' Event

Hey there! Here's the code I've been working on: HTML : <html> <body> <form enctype="multipart/form-data" method="post" name="image"> <input onchange="test();" ...

Retrieve Vuejs template by either module or ID

In my .Vue file, I have defined a template along with its subcomponents with the intention of allowing customers to override this template without needing to modify any javascript code. If there exists an element with id="search-result", then that element ...

Strain the empty elements from the array object

I am trying to remove an array object using the traditional method: delete subDirectories[index]. However, after deleting, the object changes to [empty]. I have tried filtering for undefined, bool, and NaN, but nothing seems to work. This issue is within a ...

Using ESP8266 as a client and setting up an AJAX web server

My goal is to use ESP8266 as a client that will be controlled by a server running on my computer. I want the server to send commands to the ESP8266 using AJAX, and for the ESP8266 to respond to these commands and also be able to send data back to the ser ...

Can you explain the distinction between incorporating and excluding the keyword "await" in the following code snippets?

Currently, I'm diving into an MDN article that covers async/await. I've grasped the rationale behind using the async keyword preceding functions, yet there's a bit of uncertainty regarding the await keyword. Although I've researched "aw ...

What is the best way to persist my data on a page when I navigate to a different page in React.js?

Currently, I am utilizing Material UI tabs with 8 pages as components. Each page contains input areas, and when I switch between tabs, the data in the inputs gets cleared. I want to retain this data even when moving to another tab. How can I achieve this ...

Ways to remove specific characters from the escape() function in express-validators

When using the check method from express-validator to validate user input, I'm curious if there's a way to exclude certain characters from the test. For example, currently I have: check("profile.about").trim().escape() This code snippet convert ...

Implementing dynamic watchfile listeners in Node.js upon client connection and appropriately removing them thereafter

Currently, during a client connection event, a file is being watched. However, when a second client connects, the same file is watched again using fs.watchFile(). Upon a client disconnecting event, the file is unwatched using fs.unwatchFile(). This means ...

Rendering a page using React and NextJS with server-side rendering to retrieve and showcase dynamic data

At the moment, I am utilizing client-side rendering on a page that fetches data via an API and then presents this raw data. 'use client' import React, { useState, useEffect } from 'react' import axios from 'axios'; const Sol ...

Is it possible to declare language features in Typescript? For example, changing `!variable` to `no variable`

Can Typescript language features be declared within the app's source code? I want to enhance code clarity by implementing a small feature. Modified Null Test if (no userDetails) { // handle null } This new null test syntax is a little more conc ...