1

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!

4
  • What is the value of result when you get an error on the server side? Try: console.log(result) in the JavaScript to see what's going on. Commented Oct 14, 2014 at 3:57
  • Getting: Object { readyState=4, responseText="You must be logged in to vote.", status=403, more...} Commented Oct 14, 2014 at 8:04
  • 2
    Are you parsing your response to JSON ? Shouldn't you trying to access to the result key you need ? Something like result['responseText'] ? Commented Oct 14, 2014 at 8:47
  • 1
    @Liarez - that did it, thanks! If you want to answer the question, I'll mark your answer as correct. Appreciate if you could add a short explanation too of the change. Commented Oct 14, 2014 at 23:04

1 Answer 1

2

There is 2 things you should remember using AJAX:

  1. If you receive a dictionary through answer, access to the key you need
  2. Parse the response AJAX is receiving

Access to the key you need

It seems you're sending a dictionary through your variable result. You receive the AJAX response through:

...   ...   ...
success: function(result)
    {
        $('#custom-post-message').html(result);
        $('#custom-post-rating').load('/post/{{ id }}/rating/');
    },

...  ... ...

And you're doing $('#custom-post-message').html(result); but result is a dictionary:

result = { readyState=4, responseText="You must be logged in to vote.", status=403, more...}

and you need to access using a key like:

result['responseText'] 
result['readyState']
result['status']

Parse the response AJAX is receiving

This mean that when you send the response to AJAX function you can pass a string . You need to send the response parsed to JSON.

In my Django projects, I use a JSON function to parse ( json.dump(variable) ) and json.loads(variable) to unparse

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.