[英]url 404 Not Found ajax javascript laravel
當我單擊此按鈕轉到函數 addToCart()
<input type="submit" class="btn btn-primary" id="ads value="add to cart">
它返回一個錯誤
請求網址:http: //127.0.0.1 :8000/cart/data/store/
請求方法:POST
狀態碼:404 未找到
我不知道為什么請幫忙
這是我的代碼
我的按鈕
<input type="submit" class="btn btn-primary" id="ads" value="add to cart">
和我的ajax代碼
$('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,
},
});
});
和我的路線
Route::post('cart/data/store/{id}',[App\Http\Controllers\frontend\CartController::class,'addtocart']);
查看您的 HTML,您正在編寫以下內容:
<input type="submit" class="btn btn-primary" id="ads value="add to cart">
你得到一個 404 因為確實沒有沒有 ID 參數的路由。 您根本沒有將它與您到達那里的 HTML 標記一起傳遞。 因此,修復語法將解決您的問題。
它應該如下所示:
<input type="submit" class="btn btn-primary" id="ads" value="add to cart">
我認為你的路線不正確。
Route::post('/cart/data/store/{id}',[App\Http\Controllers\frontend\CartController::class,'addtocart']);
$.ajax 調用下面給出的 url:
'/cart/data/store/'+productid
替換這個
var productid = $('productid').text();
有了這個
var productid = $('#productid').text()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.