when i click at this button to go to the function addToCart()
<input type="submit" class="btn btn-primary" id="ads value="add to cart">
it returns an error the
Request URL: http://127.0.0.1:8000/cart/data/store/
Request Method: POST
Status Code: 404 Not Found
i dont know why please help
here is my code
my button
<input type="submit" class="btn btn-primary" id="ads" value="add to cart">
and my ajax code
$('body').on('click','#ads',function(){
var productname = $('pname').text();
var productid = $('productid').text();
var color = $('#color option:selected').text();
var size = $('#size option:selected').text();
var quantity = $('#qty').val();
$.ajax({
url: "cart/data/store/"+productid,
type: "POST",
dataType: 'json',
data:{
color:color,
size:size,
quantity:quantity,
productname:productname,
},
});
});
and my route
Route::post('cart/data/store/{id}',[App\Http\Controllers\frontend\CartController::class,'addtocart']);
Looking at your HTML, you are writing this:
<input type="submit" class="btn btn-primary" id="ads value="add to cart">
You're getting a 404 because there is indeed no route without the ID parameter. You're simply not passing it with the HTML tag you got there. So, fixing the syntax would fix your issue.
It should look like this:
<input type="submit" class="btn btn-primary" id="ads" value="add to cart">
I think your route is incorrect.
Route::post('/cart/data/store/{id}',[App\Http\Controllers\frontend\CartController::class,'addtocart']);
$.ajax call the url given below:
'/cart/data/store/'+productid
replace this
var productid = $('productid').text();
with this
var productid = $('#productid').text()