Not even sure if this is the right way to title the question. I know I'm making my AJAX calls all wrong... here's what I'm doing right now (I'm using ASP.NET MVC backend by the way):
I use jQuery.ajax
to post some data to an action, which will load a view that I capture in the response. It's basically providing some data to an action, and taking some HTML back.
Let's say I wanted to create an Ajax call that is validated. How do I get back the validation messages on error? For example, an Ajax login form. If the user cannot be validated, I'd like to tell them that... instead of simply doing nothing.
Sorry for the naive question, I just don't know how to word it right to find the solutions on Google.
Thank you in advance!
I've blogged about this same situation. Maybe it will help you in your situation as well. It's basically about having a custom exception class and an exception action attribute that will return the error. So you're able to use the
success
anderror
options of the$.ajax
call. Which is in my opinion the right way of doing it.$.ajax
call does provide functionality to handle success as well as error responses. No need to always return a success with an error flag set that you have to check on the client.I'm not sure whether I fully understood what you mean. But your example of a validation message on an error, wouldn't that work like the following:
Hope that helps.
You'll need to return multiple pieces of information for your response. Luckily, you can do that easily using JSON, and jQuery will automatically handle it for you if you tell it the response type is json. The object that you get into your ajax callback function will contain all the pieces of data you need as different properties.
I would suggest getting into the habit of returning a status code of "success" or "failure" with each ajax call, and a collection of errors with it. See this awesome blog post for more information on what I mean.
The reason for this is that an ajax call will always fundamentally "succeed", unless the server actually couldn't process the request and returned a failure http status code. If the result of the request is something like a validation error, but the server still returns some kind of text response, then the ajax call is still considered to have succeeded, even though the application operation failed.
So if, in your action method, instead of returning your html data in an action result, if you returned an object like this:
This will translate into a javascript object that has properties ".Success", ".Data" and ".Errors".
So if your validation code had populated the Errors array with all the validation errors, it would be easy for your ajax callback function to
determine that the intended purpose of the call had failed, because the SUCCESS property was set to "failure".
Get all the relevant error strings.
You could do this easily with this kind of pattern in your action methods:
First of all if you want AJAX call to return the data you are planning to use in your scripts (and by use I mean something more complicated then displaying that data in a
div
), you should probably return JSON data instead of HTML from your server. JSON is actually a JS object so it can be used in JS the moment it is received.So if you return the data i.e.
You can use something like this to use that data.
});