I have developed simple cart system in laravel for my local client, it has no any issue on my local server but when I publish it on my live server which is infinity free hosting it has multiple issues. When I try to add to cart the product on home page it is showing 404 error, I am using ajax request.
JS CODE:
jQuery.ajax({
url: "/add_to_cart",
method: "post",
data: jQuery('#formAddToCart').serialize(),
success: function(result){}
my home page url is "new_shop/" and when I remove the "/" from my js code url it is working perfectly but then it shows error on my product detail page where I am using same function and the url of product detail page is "product-name" like this without "/"; product detail page add to cart is also not working with "url: "/add_to_cart"," this url. I don't know what the confusion is as it is working perfectly on my local server. I think it may be issue of free hosting. Any assistance will be appreciated.
ROUTE WEB.PHP CODE:
Route::post('add_to_cart',[FrontController::class,'add_to_cart']);
You are doing it in a wrong way from js code.
You should use name for your routes like this
Route::post('/add_to_cart',[FrontController::class,'add_to_cart'])->name('cart.add');
Now call it from the js code like this
jQuery.ajax({
url: "{{route('cart.add')}}",
method: "post",
data: jQuery('#formAddToCart').serialize(),
success: function(result){}
})
As I am not sure you have group for your route or not You can try this also
jQuery.ajax({
url: "{{URL::to('/add_to_cart')}}",
method: "post",
data: jQuery('#formAddToCart').serialize(),
success: function(result){}
})
And make sure that you are passing csrf token in your form data.
This way you don't need to worry about your methodName as well as your url. You just need to provide a name to your routes