On a Laravel driven site, there is a jquery script that controls the quantity and total price of an item in the cart. I am trying to rewrite jquery ajax in cart with pure js fetch. However, there is no way to pass body to fetch in the get request! the required parameters (quantity, product type) are passed to the body. If you put the body, the script swears that the body should not be. If you change body to data (or any other word). The request passes, but returns default zeros in values, as if this parameter was not passed. If you change the get method to post, then it gives a 404 error. Tell me how can I resolve the issue?
old script
$.ajax({
type: "get",
url: "{{ route('ajax.set.product.count') }}",
data: {
product_type: product_type,
product_id: product_id,
new_count: new_count,
_token: "{{ csrf_token() }}"
},
dataType: "json",
success: function(data) {
console.log(data);
if (data.error == 'no') {
$('#productcount-1-' + index).val(data.new_count);
$('#productprice-1-' + index).text(data.price_product);
$('#productcount-2-' + index).val(data.new_count);
$('#productprice-2-' + index).text(data.price_product);
$('#price_all').text(data.price_all);
if (data.poil_count > 0) $('.pcount').text(data.poil_count);
}
}
});
new script
fetch('{{ route('ajax.set.product.count') }}', {
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest"
},
method: 'get',
credentials: "same-origin",
body: JSON.stringify({
product_type: product_type,
product_id: product_id,
new_count: new_count,
_token: "{{ csrf_token() }}"
}),
}).then(function(response) {
return response.json();
}).then(function(data) {
console.log(data);
if (data.error == 'no') {
document.getElementById('productcount-1-' + index).value = data.new_count;
document.getElementById('productprice-1-' + index).textContent = data.price_product;
document.getElementById('productcount-2-' + index).value = data.new_count;
document.getElementById('productprice-2-' + index).textContent = data.price_product;
document.getElementById('price_all').textContent = data.price_all;
if (data.poil_count > 0) document.querySelector('.pcount').textContent = data.poil_count;
};
})