I’m looking to parse errors that are returned by my server in xml after an unsuccessful ajax request. The xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error>Full name can't be blank</error>
<error>Email can't be blank</error>
</errors>
I am simply trying to build a ul tag from the xml with an li tag for each error in the xml response. The response should be processed in a way which outputs this HTML and appends it to a div with the id of “contactErrors”:
<ul>
<li>Full name can't be blank</li>
<li>Email can't be blank</li>
</ul>
Currently I have the following jQuery, but I cannot figure out how to parse the HTTP 422 (form errors/unprocessable entity) response properly.
$("#new_message").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "messages.xml",
data: $(this).serialize(),
cache: false,
statusCode: {
201: function(data) {
$("#new_message").slideUp();
$("#contactSuccess").fadeIn(2500);
},
422: function(data) {
// XML response should be processed here and can be accessed by calling $(data)
}
}
});
});
Any help is much appreciated!
Why not JSon? For XML you can return XML in your action with