joestump / django-ajax

A simple framework for creating AJAX endpoints in Django.
http://github.com/joestump/django-ajax
BSD 3-Clause "New" or "Revised" License
260 stars 54 forks source link

AJAXError() is not JSON serializable #12

Closed ashchristopher closed 13 years ago

ashchristopher commented 13 years ago

When running through your example in your README, it returns AJAXError(404, 'Invalid user.'). When you actually return the AJAXError object, you get an _AJAXError() is not JSON serializable_ error.

Using this example code:

class MyEndpoint(BaseEndpoint):
    def __call__(self, request):
        try:
            user = User.objects.get(pk=int(request.POST['user']))
        except User.DoesNotExist:
            return AJAXError(404, 'Invalid user.')
        return self._encode_record(user)

my_endpoint = MyEndpoint()

In my java script I just tested with:

$.post('/ajax/trip_summary/my_endpoint.json', {
    user: 8123912312,
});
joestump commented 13 years ago

Great catch. That should be raise AJAXError instead. I think this is just a bug in the documentation, but I can also make it handle returning an AJAXError.

joestump commented 13 years ago

Just fixed this in fa36381ccd09ea3aad7657fbf45ca910681f7b41 and 3c9bbb335c652115daa3e746e070c0b2343f329f.