0

I'm beginner with Laravel and JQuery.

I have this code:

<ul>
   <li class="file-id-46">- <a
      href="http://test.test/upload/other/1c186a1229c8c502c9bae2794b52a6f0.pdf"
      target="_blank">upload/other/1c186a1229c8c502c9bae2794b52a6f0.pdf</a>
      <a href="#" class="red removeThisFile"
         id="46">[delete file]</a>
   </li>
   <li class="file-id-47">- <a
      href="http://test.test/upload/other/65b13b47f09fc785d901e43e7864bab2.pdf"
      target="_blank">upload/other/65b13b47f09fc785d901e43e7864bab2.pdf</a>
      <a href="#" class="red removeThisFile"
         id="47">[delete file]</a>
   </li>
   <li class="file-id-48">- <a
      href="http://test.test/upload/other/19846324513791f79013fb8e5a7854d8.pdf"
      target="_blank">upload/other/19846324513791f79013fb8e5a7854d8.pdf</a>
      <a href="#" class="red removeThisFile"
         id="48">[delete file]</a>
   </li>
   <li class="file-id-49">- <a
      href="http://test.test/upload/other/fca69b7c02ae223cd167202368d3a555.pdf"
      target="_blank">upload/other/fca69b7c02ae223cd167202368d3a555.pdf</a>
      <a href="#" class="red removeThisFile"
         id="49">[delete file]</a>
   </li>
   <li class="file-id-50">- <a
      href="http://test.test/upload/other/2a1f88cd9f223a423a29594be3d8ce7a.pdf"
      target="_blank">upload/other/2a1f88cd9f223a423a29594be3d8ce7a.pdf</a>
      <a href="#" class="red removeThisFile"
         id="50">[delete file]</a>
   </li>
   <li class="file-id-51">- <a
      href="http://test.test/upload/other/dbe585a9716bd417437be6b0d5695a6a.pdf"
      target="_blank">upload/other/dbe585a9716bd417437be6b0d5695a6a.pdf</a>
      <a href="#" class="red removeThisFile"
         id="51">[delete file]</a>
   </li>
   <li class="file-id-52">- <a
      href="http://test.test/upload/other/585b8aa2cfd6a871eb3ed0e1c6d5e2b9.pdf"
      target="_blank">upload/other/585b8aa2cfd6a871eb3ed0e1c6d5e2b9.pdf</a>
      <a href="#" class="red removeThisFile"
         id="52">[delete file]</a>
   </li>
</ul>


$(".removeThisFile").click(function () {
                    var id = $(this).attr("id");
                    $('.file-id-' + id).hide();
                    $.ajax({
                        url: 'http://test.test/psCMS/removeOtherFile?id=52',
                        type: 'get',
                        dataType: 'json',
                        _token: $('meta[name="_token"]').attr('content'),
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        cache: false,
                        success: function (response) {
                            $(this).hide();
                        }
                    });

Generally, jquery correctly sends a query to php (Laravel). The problem occurs in deleting files. The first file is deleted correctly. The next file can not be slept. I suspect that I have an invalid X-CSRF-TOKEN token after deleting the previous file.

How can I fix this problem?

1
  • What is '_token'? Is it simple laravel api token? Commented May 26, 2019 at 14:33

2 Answers 2

1

In js code:

$(".removeThisFile").click(function () {
    var id = $(this).attr("id");
    $('.file-id-' + id).hide();
    $.ajax({
        url: 'http://test.test/psCMS/removeOtherFile/'+id,
        type: 'get',
        dataType: 'json',
        cache: false,
        success: function (response) {
            $(this).hide();
        }
    });
});

In route file (web.php):

Route::get('/psCMS/removeOtherFile/{id}', 'YourController@removeOtherFile');

In controller:

public function removeOtherFile($id){

    $my_id = $id;
    // here is your id 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Its possible that you may have some x-csrf issues, as this doesn't look like what I'm used to seeing (though it may work just fine). You might have better luck using the @csrf in the top of your form, and calling a general header in an ajax setup method.

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

However, to directly answer you question, I believe the issue is much easier - I think it is a typo. You've hard coded the id. Try changing this line:

url: 'http://test.test/psCMS/removeOtherFile?id=52',

to:

url: 'http://test.test/psCMS/removeOtherFile?id='+id, // <--- note the variable

Comments

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.