[Answer]-'data' function not hit in seemingly successful jquery post

0👍

I found out the issue – it’s pretty stupid on my part. My click target was an href link with ‘#’ and because I didn’t have a preventDefault in my click function, it was refreshing before the data post and I was getting an Error Code 10054: Connection reset

1👍

There are a variety of tools to help debug these types of situations.

If I were going to start debugging this issue, I would first look into any error logs that may exist on your server and see if any Exceptions are being raised. If all looks well there, I would use a tool called ‘pdb’ (https://docs.python.org/2/library/pdb.html) and insert a break point at the start of the view and step through each line of code to verify all if working correctly…

def delete_visit(request):
        import pdb; pdb.set_trace()
        v_id=request.POST['v_id']
        if v_id:
            v_obj=Visit.objects.get(id=v_id)
            print v_obj.id, 'it works!' # this all prints
            v_obj.delete()
        return HttpResponse('a')

If all checks out well there, then it’s time to review the client side code and see what is going on there. Most modern browsers come with consoles to inspect and monitor all network requests and javascript code. I would start by taking a look at the logged network requests and see what your ajax request is actually returning. It may be returning a non success code such as 500 or 404. If this is the case then the jQuery callback function in which you have for the $.post function won’t be called because that is only executed upon a successful response.

In regards to if there is a better way to debug ajax, try inserting the statement ‘debugger;’ or placing a breakpoint within your browsers development tools in order to step though executing just like the way pdb does.

Leave a comment