404 not found in console laravel and ajax

if I click heart icon to add product to wishlist it works (laravel and ajax ) but what’s the problem or what I have to check if I get 404 not found in console :

  1. Request URL:

http://localhost/%7B%7BRoute('wishlist.add')%7D%7D

  1. Request Method:

POST

  1. Status Code:

404 Not Found
and thank you very much

$(document).ready(function(){
    
    loadwishlist();
   

    $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

   


    $('.add-to-wishlist').click(function(e){
        e.preventDefault();


        @guest()
               swal("login");
            @endguest

       var product_id=$(this).closest('.product_data').find('.product_id').val();
            
           
            $.ajax({
                type: 'post',
             
                url: "{{Route('wishlist.add')}}",
                data: {
                    'product_id': product_id,
                },
                success: function (response) {
                  
                   swal(response.status);
                   loadwishlist();
                   
                   
                }
            });
        });
    });

	
	</script>
@endpush

Is that the request as it is being logged in the network tab of the dev tools?

If so, you’ll need to somehow evaluate {{Route('wishlist.add')}} and make the request to whatever it evaluates to.

Thank you thank you very much James_Hibbard,


sorry but I don’t understand what you mean can you explain with exemple or code because I don’t know what to do ( sorry but I tried to see some tutorials and follow them to learn something new for me and to learn from my mistakes) and please advice me what to do if it happen again with other code thank you very much
wishlistController:


    public function store(Request $request)
    {

      $product_id= $request->product_id;
if( Wishlist::where('user_id',auth()->id())->where('product_id',$product_id)->exists())
{
return response() -> json(['status' => "This Product is already in your wishlist "]);
}
else
{
       $wishlist = new Wishlist;

       $wishlist->user()->associate($request->user());

       $wishlist->product_id = $product_id;
        $wishlist->save();
        return response() -> json(['status' => "Product added to wishlist"]);
     
   }

replace with

{{ route('wishlist.add') }}
  1. Spaces are required between {{ }} and their content;
  2. Helper method route() starts with lowercase.

thank you very very much veocode
I did what you said and I tried to:clear browsing data , php artisan cache:clear,php artisan view:clear,php artisan optimize ,restart wamp and I removed jquery code from products.blade.php and I tried to click heart button It always the same error appear I don’t know why and thank you very much and sorry

If you’ve replaced it and nothing has changed, then probably:

  1. you’ve replaced it in a wrong view file - search for {{Route('wishlist.add')}} across other views;

or

  1. view has been cached - run php artisan view:clear to drop the cache.

thank you very much veocode It works now and I tried to:clear browsing data , php artisan cache:clear,php artisan view:clear,php artisan optimize ,restart wamp and I removed jquery code from products.blade.php but I don’t know why It just works after I closed my PC and opened it again because it is not ideal to restart my PC every time I tried to fix code or change it with other code(when it dosen’t work) and thank you very very much

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.