Skip to content

Instantly share code, notes, and snippets.

@dougvk
Created July 17, 2012 08:38
Show Gist options
  • Save dougvk/3128062 to your computer and use it in GitHub Desktop.
Save dougvk/3128062 to your computer and use it in GitHub Desktop.
Example Ajax POST to django server
$(document).on("submit", "#test_form", function(e) {
e.preventDefault();
var self = $(this),
url = self.attr("action"),
// Note: CSRF token already added to HTTP Headers
ajax_req = $.ajax({
url: url,
type: "POST",
data: {
// Send the form data with POST request
name: self.find("#id_name").val()
},
success: function(data, textStatus, jqXHR) {
// Some helper function we define that raises a sleek,
// informative box.
django_message("TestObj saved successfully.", "success");
},
error: function(data, textStatus, jqXHR) {
var errors = $.parseJSON(data.responseText);
$.each(errors, function(field, value) {
if (field === "__all__") {
// If all are in error, do something special?
django_message(value[0], "error");
} else {
// Some helper function we define that raises
// sleek, informative error message(s)
apply_form_field_error(field, value);
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment