---
title: "What is the role of the OnSuccess and OnFailure callbacks in an Ajax request in MVC?"  
description: "What is the role of the OnSuccess and OnFailure callbacks in an Ajax request in MVC?"  
author: "Revati S Misra"  
published: 2023-04-18  
updated: 2025-01-20  
canonical: https://www.mindstick.com/forum/157899/what-is-the-role-of-the-onsuccess-and-onfailure-callbacks-in-an-ajax-request-in-mvc  
category: "ajax"  
tags: ["ajax", "jquery", "mvc"]  
reading_time: 2 minutes  

---

# What is the role of the OnSuccess and OnFailure callbacks in an Ajax request in MVC?

What is the [role](https://yourviews.mindstick.com/audio/1254/the-role-of-visualization-in-achieving-your-goals) of the [OnSuccess](https://www.mindstick.com/interview/34343/explain-how-client-side-events-like-onsuccess-onfailure-and-onbegin-work-in-an-ajax-form) and OnFailure [callbacks](https://www.mindstick.com/articles/337167/how-do-you-handle-asynchronous-operations-in-javascript) in an [Ajax request](https://www.mindstick.com/forum/157891/what-is-the-role-of-the-jsonresult-class-in-mvc-how-can-it-use-to-return-data-to-an-ajax-request) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)?

## Replies

### Reply by Khushi Singh

When using [Ajax](https://www.mindstick.com/articles/1541/check-availability-of-user-name-or-email-using-asp-dot-net-ajax-and-jquery) requests in an MVC application, the `OnSuccess` and `OnFailure` methods are probably quite familiar to us, as they handle the response from the server after the [request](https://www.mindstick.com/blog/255/post-get-and-request-function-in-php) has occurred.

**1. OnSuccess Callback**\
`OnSuccess` is called when Ajax request has been completed successfully and the server has responded to the request made. This callback enables you to manage the server's response and update the user interface.

## Typical usage:

Changing the textual content of an HTML page.\
Displaying success messages.\
Performing other Java script logic from the execution of the response data output.

## Example:

```javascript
$.ajax({
   url: '/someurl',
   type: 'GET',
   success: function(response) {
       // Handle the response here
       console.log("Request was successful:", response);
       $('#someElement').text(response.message);
   }
});
```

In this example, the `success` function will run if the request to the server is completed successfully, and the `response` parameter contains the data returned by the server.

**2. OnFailure Callback**\
The OnFailure callback is performed when the Ajax request is unsuccessful, such as when a network connection is lost when the server returns an error status (404, 500, or other), or in case of some other unsuccessful result. This callback enables you to correct errors and enlighten the user of the existence of a particular problem.

## Typical usage:

Displaying error messages.\
Managing such eventualities as server-related issues, and timeouts.\
Recording accidents when troubleshooting.

## Example:

```javascript
$.ajax({
   url: '/someurl',
   type: 'GET',
   error: function(xhr, status, error) {
       // Handle the error here
       console.log("Request failed:", error);
       alert("Something went wrong! Please try again later.");
   }
});
```

In this case, if the request fails, the error function is called which contains information about the error, the status, and a possible error message.\

\


---

Original Source: https://www.mindstick.com/forum/157899/what-is-the-role-of-the-onsuccess-and-onfailure-callbacks-in-an-ajax-request-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
