ASP.NET MVC - the right way to handle ajax actions without returning an object

I have a controller action that does some work in the database and then completes when it is complete. This action is called through a jQuery ajax function with the dataType type set to 'json'.

If I set the return type of the action to void, everything will work fine, except that Firefox will display an error in the console that says: "no element found".

It makes sense that Firefox will throw this error if it expects XML to return. However, even when I change the dataType property of the ajax call to β€œtext”, I still get the error. To get rid of the error with the void return type, I would have to set the Response ContentType to "text / html". Or I can set the return type to JsonResult and return a new [empty] JsonResult object.

I am sure there are several ways for this error to disappear, but I wanted to know how to handle actions correctly without returning the values ​​called through ajax.

If that matters, I also use the asynchronous controller action pattern.

public void DoSomethingAsync(SomeJsonObjectForModelBinding model) { // do some database things } public void DoSomethingCompleted() { // nothing to do... // what should my return type be? // do I need to set the content type here? } 
+7
source share
2 answers

I know this doesn't exactly answer your question, but I would say that you should always have a return value returned from an AJAX call or web service. Even if you only report that the operation was successful or otherwise returned an error message (message).

I often define a class as follows:

 public class JsonResultData { private bool _success = true; public bool Success { get { return _success; } set { _success = value; } } public object Value { get; set; } public List<string> Errors { get; set; } public JsonResultData() { this.Errors = new List<string>(); } } 

And then use it to return data or any other call metadata to the JsonResultData wrapper as follows:

 return new JsonResult { Data = new JsonResultData { Value = returnValue, Success = true } }; 
+11
source

I cannot comment because of my reputation, but I still wanted to contribute to the confusion in Kon .

In the application, I found all exceptions in ActionMethod, installed HttpStatusCode and added an error message in the response. I retrieved the message in the Ajax error function and showed it to the user.

Everything worked fine until the application got to the intermediate server, which had some settings that did not allow returning a message in an erroneous response. Instead, some standard Html was passed, resulting in a JS error processing the response.

In the end, I had to rewrite all of the exception handling returning my application errors as a successful Ajax call (actually this), and then they differ in the Ajax success function, as it should be.

You should not mix system level and application level feedback. You may not be able to control feedback at the system level as you need.

0
source

All Articles