Monday, April 6, 2015

ajax post request: simple

An ajax post request can be as simple as any form in django. Just remember the csrf token!
// uses jquery
$.ajax({
type: "POST",
url: "{% url 'attachment-object-coupling' project.slug %}",
data: {
attachment: attachments.attr("id"), // attachment instance_id
object: objects.attr("id"), // object instance_id
csrfmiddlewaretoken: '{{ csrf_token }}' // if not configured to include token in every post
},
success: function(data) {
updateDOM(data); // define elsewhere
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error, status = " + textStatus + ", " +
"error thrown: " + errorThrown
);
}
});