[Answered ]-Ajax error return – not returning variable

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

Leave a comment