I am new to ajax so hopefully this is a simple oversight.
I have the following code for upvotes, based off the django updown app
$(document).ready(function() {
$("#custom-post-up").submit(function(event){
event.preventDefault();
$form=$('#custom-post-up');
var datastring = $form.serialize();
$.ajax({
type: "POST",
url: $form.attr('action'),
dataType: 'html',
data: datastring,
success: function(result)
{
$('#custom-post-message').html(result);
$('#custom-post-rating').load('/post/{{ id }}/rating/');
},
error: function(result)
{
$('#custom-post-message').html(result);
}
});
return false;
});
});
The problem - the error message doesn't get produced. if I put in a string instead, it works fine. eg:
error: function(result)
{
$('#custom-post-message').html("error");
}
Also, the success message works fine too.
The error message I am trying to get is from the view in the updown app:
def authentication_required_response(self, request, context):
response = HttpResponse('You must be logged in to vote.')
response.status_code = 403
return response
The success message from this view works fine:
def rating_changed_response(self, request, context):
response = HttpResponse('Vote changed.')
return response
Thanks!
resultwhen you get an error on the server side? Try:console.log(result)in the JavaScript to see what's going on.JSON? Shouldn't you trying to access to the result key you need ? Something likeresult['responseText']?