How to send data in GET method from client side to server side - ajax
I'm trying to send two dates, start date and end date to return back between two dates , here is what i tried but doesnt work,
$(document).ready(function(){
const date_inputs = new FormData();
$('#date_form').submit(function(e){
date_inputs.append('from',document.getElementById('from').value)
date_inputs.append('to',document.getElementById('to').value)
e.preventDefault();
})
console.log(date_inputs)//returns empty
function dateTimePrices(){
$.ajax({
type:'GET',
url:'/prices/dateTime/data',
data:date_inputs,
success:function(data){
const datetimes = data;
spinner.setAttribute('hidden',true);
var k = '<tbody>';
if(datetimes){
k+= '<tr>';
k+= '<td>' + datetimes["all_qnt"] + '</td>';
k+= '<td>' + datetimes['all_price'] + '</td>';
k+= '</tr>'
}else{
k+= '<td class="p-2 text-xs border border-purple-900 md:text-base textpurple" colspan=2>not found</td>'
}
k+='</tbody>'
document.getElementById('datetime_prices_list').innerHTML = k
}
})
}
dateTimePrices();
})
<form action="" method="GET" id="date_form">
<div class="col-11 p-1 mt-1 mx-auto text-center row rtl ">
<p class="col-12 col-sm-6 mx-auto text-left row">
from
<input type="date" class="form-control col-9 mr-1" name="from" id="from">
</p>
<p class="col-12 col-sm-6 mx-auto text-right row">
to
<input type="date" name="to" class="form-control col-9 mr-1" id="to">
</p>
<button type="submit" class="btn btn-info >search</button>
</div>
</form>
i also tried this to create the dataForm, const date_inputs= (new Date(fromDate)).toUTCString();
but it say :
Property 'append' does not exist on type 'string'.ts(2339)
is there away to add date input into dataForm please
and here is my django view code
def priceByDateTime(request):
start = request.GET.get('from')
end = request.GET.get('to')
print(start,end)#
if start and end:
datetimes = MyModel.objects.filter(invoice__created_at__range=(start,end)).annotate(
total_price=Sum(
(F('price')) - F('discount'),output_field=DecimalField(max_digits=20,decimal_places=3))
).annotate(
total_quantity=(
Count('pk')
)
).aggregate(
all_price=Sum(F('total_price')),
all_qnt=Sum(F('total_quantity'))
)
else:
datetimes = MyModel.objects.all().annotate(
total_price=Sum(
(F('price')) - F('discount'),output_field=DecimalField(max_digits=20,decimal_places=3))
).annotate(
total_quantity=(
Count('pk')
)
).aggregate(
all_price=Sum(F('total_price')),
all_qnt=Sum(F('total_quantity'))
)
return JsonResponse(datetimes,safe=False)
@login_required
def queryTemplate(request):
return render(request,'myapp/prices.html')
thank you in advance .. i very appreciate any idea ...
I never worked with jquery, only pure js and ajax, so i will write a example in js and ajax
in your script tag in your template you will have something like
<script>
function filterDates() {
const dateFrom = document.getElementById('from').value
const dateTo = document.getElementById('to').value
const request = new Request(`path_url?from=${dateFrom}&&to=${dateTo}&&`, {method: 'GET'})
fetch(request, {
method: 'GET',
mode: 'same-origin'
}).then(
function (response) {
console.log(response)
if (response.status === 200) {
console.log("success")
} else {
console.log("error")
}
}
)
}
</script>
Something i forgot to say about the formData, it maybe appers to be empty if you try to console.log(formData) but it can have data on it, i don't know why but it happened to me in the pass