[Answer]-Ajax is giving back all the html instead of the variable in Django?

1đź‘Ť

This line:

if request.GET:

checks to see if there are any GET parameters. There aren’t, because you’re not sending any. The URL is just /home. You could use if request.method == 'GET', but I think you’re checking for the wrong thing here: a normal request (not Ajax) will also be GET.

What you should do is send the HTTP_X_REQUESTED_WITH header as “XmlHttpRequest”, then check request.is_ajax() in the view. Or, as recommended, use a library like jQuery – which sets that for you automatically.

0đź‘Ť

The proper way to detect request type is if request.method == 'GET', instead of if request.GET.

👤niconoe

Leave a comment