Reputation: 3
I tried to get the data I send via Ajax with request.GET.get, but this does not work. Data is always None. I really appreciate any kind of help!
This is my jQuery/Ajax code:
$(document).ready(function(){
$(".jcf-select.jcf-unselectable").on('click', function(){
var sel = $('#sort').children("option:selected").val();
console.log(sel)
$.ajax({
url: '/filter-data',
data: sel,
dataType: 'json',
success:function(res){
console.log(res);
$("#filter_products").html(res.order);
}
});
});
});
This is my view:
def filter_data(request):
if request.is_ajax:
query = []
data = request.GET.get('data')
if data == "gpv":
query = data.order_by("price_with_shipping")[:150]
elif data == "gp":
query = data.order_by("lowest_price")[:150]
elif data == "lp":
query = data.order_by("liter_price")[:150]
elif data == "tz":
query = data.order_by("-lowest_price")[:150]
t = render_to_string('homepage/ajax-products.html', {'order': query})
return JsonResponse({'order': t})
Upvotes: 0
Views: 42
Reputation: 71
data must be an object in ajax. for example:
$.ajax({
url: '/filter-data',
data: {'key-data':sel},
dataType: 'json',
success:function(res){
console.log(res);
$("#filter_products").html(res.order);
}
and in your django veiw:
data = request.GET.get('key-data')
Upvotes: 1