skip to Main Content

I am having an issue with making a post request in my project, like whenever i am trying to make a request using a button in my application i am having an error with tp://localhost/delete-comment 404 (Not Found) send @

This is my code in which i am making the request:

<script>

        $(document).ready(function(){

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


            $(document).on('click', '.deleteComment', function () {


                 if(confirm('Are you sure you want to delete this comment?'))
                 {

                         var thisClicked = $(this);
                         var comment_id = thisClicked.val();
                         $.ajax({
                            type:'POST',
                            url: '/delete-comment',
                            data: {
                                'comment_id': comment_id,
                                '_token': $('meta[name="csrf-token"]').attr('content')
                            },
                            success:function(res){
                                if(res.status == 200){
                                    thisClicked.closest('.comment-container').remove();
                                    alert(res.message);
                                }
                                else{
                                    alert(res.message);
                                }
                            }
                         });


                }
            });
        });
    </script>

And this is my web.php file:

Auth::routes();

Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');

Route::get('/', [AppHttpControllersFrontendFrontendController::class, 'index']);
Route::get('tutorial/{category_slug}', [AppHttpControllersFrontendFrontendController::class, 'viewCategoryPost']);
Route::get('/tutorial/{category_slug}/{post_slug}', [AppHttpControllersFrontendFrontendController::class, 'viewPost']);

// Comment System

Route::post('comments', [AppHttpControllersFrontendCommentController::class, 'store']);
Route::post('delete-comment', [AppHttpControllersFrontendCommentController::class, 'destroy']); //here is the request i am getting

and below is my commentController.php file which have the destroy function:

<?php

namespace AppHttpControllersFrontend;

use AppHttpControllersController;
use AppModelsPost;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use AppModelsComment;
use IlluminateSupportFacadesValidator;


class CommentController extends Controller
{
    public function store(Request $request)
    {
        if (Auth::check()) {

            $validator = Validator::make($request->all(), [
                'comment_body' => 'required|string'
            ]);
            if ($validator->fails()) {
                return redirect()->back()->with('message', 'Comment Area is Mandetory');
            }

            $post = Post::where('slug', $request->post_slug)->where('status', '0')->first();
            if ($post) {
                Comment::create([
                    'post_id' => $post->id,
                    'user_id' => Auth::user()->id,
                    'comment_body' => $request->comment_body,
                ]);
                return redirect()->back()->with('message', 'Commented Successfully');
            } else {
                return redirect()->back()->with('message', 'No Such Post Found!');
            }
        } else {
            return redirect('login')->with('message', 'Login first to comment');
        }
    }


    public function destroy(Request $request) //here is the destroy function
    {
        if (Auth::check()) {
            $comment = Comment::where('id', $request->comment_id)->where('user_id', Auth::user()->id)->first();

            if ($comment) {
                $comment->delete();
                return response()->json([
                    'status' => 200,
                    'message' => 'Comment Deleted Successfully',
                ]);
            } else {

                return response()->json([
                    'status' => 500,
                    'message' => 'Something Went Wrong',
                ]);
            }
        } else {
            return response()->json([
                'status' => 401,
                'message' => 'Login to Delete this Comment'
            ]);
        }
    }
}

2

Answers


  1. To make sure you put the correct url in ajax. First, name your route:

    Route::post('delete-comment', [AppHttpControllersFrontendCommentController::class, 'destroy'])->name('deleteComment');
    

    Then:

    $.ajax({
       type:'POST',
       url: '{{route('deleteComment')}}',
    
    Login or Signup to reply.
  2. I suggest use method {{ route (”) }} to call the route.

    In your routes, must specify the name of the route

        Route::post('delete-comment', [AppHttpControllersFrontendCommentController::class, 'destroy'])->name('delete-comment');
    

    then call this in your ajax

        $.ajax({
                            type:'POST',
                            url: '{{ route('delete-comment') }}',
                            data: {
                                'comment_id': comment_id,
                                '_token': $('meta[name="csrf-token"]').attr('content')
                            },
                            success:function(res){
                                if(res.status == 200){
                                    thisClicked.closest('.comment-container').remove();
                                    alert(res.message);
                                }
                                else{
                                    alert(res.message);
                                }
                            }
                         });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search