Answer the question
In order to leave comments, you need to log in
How to get data from django with ajax?
Good afternoon. So, I am getting data to the server after clicking a button using ajax. The data comes, but I didn’t quite understand how to form the answer.
jQuery:
$.ajax({
url: '/sorter/',
type: "get",
data: {
'k': 1
},
dataType: 'text',
success: function(data){
console.log(data);
}
}
);
def sorter(request):
if request.method == 'GET':
return HttpResponse('1')
else:
return redirect('http://127.0.0.1:8000')
Answer the question
In order to leave comments, you need to log in
something like this:
from django.http import JsonResponse
def sorter(request):
if request.method == 'GET':
return JsonResponse('1')
else:
return redirect('http://127.0.0.1:8000')
Content-Type
application/json
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question