简体   繁体   English

url 404 Not Found ajax javascript laravel

[英]url 404 Not Found ajax javascript laravel

when i click at this button to go to the function addToCart()当我单击此按钮转到函数 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/请求网址:http: //127.0.0.1 :8000/cart/data/store/

Request Method: POST请求方法:POST

Status Code: 404 Not Found状态码:404 未找到

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和我的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,
      },
    });
  
});

and my route和我的路线

Route::post('cart/data/store/{id}',[App\Http\Controllers\frontend\CartController::class,'addtocart']);

Looking at your HTML, you are writing this:查看您的 HTML,您正在编写以下内容:

<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.你得到一个 404 因为确实没有没有 ID 参数的路由。 You're simply not passing it with the HTML tag you got there.您根本没有将它与您到达那里的 HTML 标记一起传递。 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: $.ajax 调用下面给出的 url:

'/cart/data/store/'+productid

replace this替换这个

var productid = $('productid').text();

with this有了这个

var productid = $('#productid').text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM