3

I'm using ajax to delete a file, but it showed 404 error. I checked route again but i don't know where is wrong! Any solution? Thanks so much!

enter image description here

My Route

Route::post('/deleteFile/ajax/{id}', 'DproductController@deleteFileAjax')->name('delete.file.ajax');

My Controller

public function deleteFileAjax($id)
{
    if (allow('delete') == true) {
        $deleteFile = DproductFile::find($id);
        $deleteFile->delete();
        return response()->json('message', 'Yes');
    } else {
        return response()->json('message', 'No');
    }
}

My View

<a href="#" data-id="{{ $dproductFile->id }}" name="{{ $dproductFile->filename }}" link="{{ route('delete.file.ajax', $dproductFile->id) }}" class="deleteClick red id-btn-dialog2" data-toggle="modal" data-target="#deleteModal">
                                                            <span class="btn-sm btn btn-danger"><i title="Delete" class="ace-icon fa fa-trash-o bigger-130"></i></span></a>

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

            $('.deleteClick').click(function (e) {
                e.preventDefault();
                var id = $(this).attr('data-id');
                console.log(id);
                $.ajax({
                    url: '/deleteFile/ajax/' + id,
                    type: 'post',
                    dataType: 'json',
                    success: function () {
                        console.log('OK');
                    }
                });
            });
</script>
12
  • 1
    do you have any prefix in your route?? and try with absolute route instead of relative route. Commented Nov 25, 2019 at 4:46
  • 404 route is not found . Can you check the route please? Commented Nov 25, 2019 at 4:51
  • zahidhasanemon, No, I haven't. What is absolute route? I don't understand Commented Nov 25, 2019 at 5:10
  • @hemant, I checked again and i don't know where is wrong :( Commented Nov 25, 2019 at 5:11
  • Please check once manually with the given route url. for example: coredev.com:8080/deleteFile/ajax/95 Is this works?! Commented Nov 25, 2019 at 5:12

4 Answers 4

3

You're using Laravel named routes ->name('delete.file.ajax').

So in AJAX

var path = "{{ route('delete.file.ajax') }}";
$.ajax({
    type: "post",
    url: path,

Personally recommended to use (URL)

Route::post('/deleteFile/ajax/{id}', 'DproductController@deleteFileAjax')->name('delete-file');

In AJAX

var path = "{{ route('delete-file') }}";
Sign up to request clarification or add additional context in comments.

Comments

1

Check the route again with command php artisan route:list.

If everything is fine with the route paths, you may have route caching issue. Do clear route cache with php artisan route:clear command.

Comments

1

Ensure that route name is the same you used

Type

php artisan route:list

and look at route name

Comments

0

Check for your $.ajax. type: 'post' will not send an ajax request as a POST request. You have to use method: 'POST' instead. Check the documentation for more customization - https://api.jquery.com/jquery.ajax/

1 Comment

Also from the same documentation... "type (default: 'GET') - An alias for method"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.