Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created July 5, 2013 09:13
Show Gist options
  • Select an option

  • Save benfoster/5933185 to your computer and use it in GitHub Desktop.

Select an option

Save benfoster/5933185 to your computer and use it in GitHub Desktop.
Handling modelstate errors when hijaxing a form
$(".hijax").submit(function (e) {
var $form = $(this),
validator = $form.data("validator");
if (!$form.valid()) {
return;
};
$.ajax({
url: this.action,
type: "POST",
data: $form.serialize(),
statusCode: {
400: function (xhr, textStatus, errorThrown) {
var errors = $.parseJSON(xhr.responseText);
validator.showErrors(errors);
}
},
error: function () {
// reset submit button
$("button[type=submit]").button("reset");
}
});
e.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment