3

If I make an ajax call to a controller.... what needs to happen in the controller so that the ajax call then calls

1) complete:
2) success:
3) error:
4) any other callbacks that exist.

For ex. I have this ajax call.

 $.ajax({
         url: "/ContactPartial/ContactUs",
         type: "POST",
         data: JSON.stringify(data),
         dataType: 'json',
         contentType: "application/json; charset=utf-8",
         complete: function () {  },
         success: function () {  },
         error: function () {  }
   });

In other words, what can I do inside /ContactPartial/ContactUs to control which of the 3 (complete,success,error) gets called after the controller code executes.

Also, how is this related to related to return Json(new {some: data});

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
parliament
  • 21,544
  • 38
  • 148
  • 238

1 Answers1

2

These three callbacks are related to the status of the Ajax call. These are called depending on success of the call. For complete details refer to the documentation

So, if the server responds with a success (200), then both the Success and the Complete handlers would be called. In the complete handler, you might put some code to dismiss a modal window (regardless of success or error), and in the success function, you might put code to let the user know the call was successful, reload a grid view, etc. Also, keep in mind that the callback functions don't have to be anonymous functions, they can be defined functions that are shared among several Ajax calls.

EDIT:

If you are wanting to force the server to generate an error, take a look at:

The HttpResponse class, specifically the StatusCode property

This SO post explains more too (generating a 401 error)

Community
  • 1
  • 1
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • thank you for that explanation. But what determines which code gets return from the server? for example, can i programmatically (in the controller) force an error code to be returned? a success code? – parliament Mar 19 '12 at 22:14
  • Only if you can force the server to return an error. The way it works is you send a request and the server responds with a status code indicated the success/failure/error of the request. – LJ Wilson Mar 19 '12 at 23:41
  • sorry but I still dont understand how to force the server to return an error. Do u mean add a ModelError to the ModelState then return the model? Perhaps a code example would go a long way. – parliament Mar 20 '12 at 02:33
  • Well, if you gave a url that didn't exist, that would cause an error. A request with credentials supplied to a website requiring authentication would also cause an error. – LJ Wilson Mar 20 '12 at 02:37
  • yes but thats all errors cause in the ajax request but doesnt explain how to force an error from inside the controller. For example: User tries to register for an account. While sending an email verification the SmtpClient throws an exception which is caught by a catch block. How can I immediately break out of the controller and return directly to the error callback (with some error message)? I must be missing something. Thanks again for your help. – parliament Mar 20 '12 at 02:54