Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to return from ASP.NET MVC action to allow jQuery ajax success event to fire?

Tags:

Right now my ajax posts all fire their Error events even if the action did not cause an error. I have an ASP.NET MVC action that looks like:

[AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult StuffToDo(int id) {      // do various things that work ...      return new JsonResult(); // i have to return something, so this... } 

On the client side I have this jQuery:

$('#actionClick').click(function() {      if (confirm('Are you sure?')) {          $.ajax({              type: "POST",              url: "/Customer/StuffToDo/<%= Model.Customer.Id %>",              contentType: "application/json; charset=utf-8",              data: "{}",              dataType: "json",              success: function() {                  ShowSuccessResult("Yay!");              },              error: function(xhr, ajaxOptions, thrownError) {                  ShowErrorResult("Boo! Message:" + xhr.responseText);              }          });       }       return false;  }); 

If the action is successful (no exceptions thrown) then I would expect the success event handler to be triggered. Instead the error event is firing. Is there something I should pass back or change in the action so the success event fires?

I realize this question is basically the same as this other question, but my error handler already has the expanded signature which solved the other person's question.

I changed the return value to Null to see if that affected anything, but no behavior change.

Its starting to look like this is an issue with HTTPS. I get multiple responses from the request. The first 2 are 401 messages and then I get a 200.

like image 819
Sailing Judo Avatar asked Feb 05 '10 16:02

Sailing Judo


People also ask

How do I return data after AJAX call success?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

What does AJAX request return?

The $.ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.

What triggers AJAX success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

Can we implement AJAX using jQuery in MVC?

Using AJAX In ASP.NET MVC. Implementation of Ajax can be done in two way in ASP.Net Application: using Update Panel and, using jQuery.


1 Answers

I believe the problem is that you're not returning anything in the JsonResult. Try:

return this.Json(string.Empty); 

and see if that works. I think that the problem is that you're returning nothing to the jQuery call rather than an empty JSON set:

{} 
like image 148
48klocs Avatar answered Oct 10 '22 04:10

48klocs