cakephp 403错误:无法加载或删除

kfgdxczn  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(142)

我正在使用cakephp 4。尝试添加一些sweetalert。当单击删除按钮时,Sweetalert会弹出,但当我单击确认删除时,它并没有删除数据。
template/layout/Users/index.php

<table>
<tr>
    <th>Username</th>
    <th>Usertype</th>
    <th>Created</th>
    <th>Action</th>
</tr>

<?php foreach ($users as $user) : ?>
    <tr>
        <td>
            <?= $this->Html->link($user->username, ['action' => 'view', $user->slug]); ?>
        </td>
        <td>
            <?= $this->Html->tag('span', $user->utype) ?>
        </td>
        <td>
            <?= $user->created->format(DATE_RFC850); ?>
        </td>
        <td>
            <?= $this->Html->link('Edit', ['action' => 'edit', $user- 
               >slug]); ?> /

            <a href="#" class="delete" data-slug="<?=$user->slug? 
               >">Delete</a>

        </td>
    </tr>
<?php endforeach; ?>

template/layout/Users/index.php

<script>

deletes = document.getElementsByClassName('delete');
Array.from(deletes).forEach((element) => {
  element.addEventListener("click", (e) => {
    let ajax_url = $(e.target).attr('data-slug');

    Swal.fire({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      icon: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
      if (result.isConfirmed) {
          $.ajax({
            method: 'POST',
            url: '/users/delete/'+ ajax_url,
            beforeSend: function(xhr){
                xhr.setRequestHeader(
                    'X-CSRF-Token',
                    <?= json_encode($this->request- 
                       >getParam('_csrfToken')); ?>
                );
            },
            success: function(response){
                if(response){
                    Swal.fire(
                        'Deleted!',
                        'Your file has been deleted.',
                        'success'
                    )
                }
                else {
                    Swal.fire({
                      icon: 'error',
                      title: 'Oops...',
                      text: 'No data deleted',
                 })
      }
            },
            error: function(e){
                console.log('error', e);
            }
          })

      } 
    })
  })
})

在我的UsersController.php中,删除方法如下所示
UsersController.php

public function delete($slug)
{
    $this->request->allowMethod(['post', 'delete']);
    $user = $this->Users->findBySlug($slug)->firstorFail();
    if ($this->Users->delete($user)) {
        $this->Flash->success("Deleted Successfully");
        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error('Unable to Delete user');
    return $this->redirect(['action' => 'index']);
}

路径文件在这里config/route.php

<?php

   use Cake\Http\Middleware\CsrfProtectionMiddleware;
   use Cake\Routing\Route\DashedRoute;
   use Cake\Routing\RouteBuilder;

  $routes->setRouteClass(DashedRoute::class);

  $routes->scope('/', function (RouteBuilder $builder) {

   $builder->connect('/', ['controller' => 'Pages', 'action' => 
'display', 'home']);

  $builder->connect('/users/delete/{slug}', ['controller' => 'Users', 
'action' => 'delete']);

  $builder->connect('/pages/*', 'Pages::display');

  $builder->fallback();

});

2vuwiymt

2vuwiymt1#

我确定这是CSRF令牌。尝试替换

<?= json_encode($this->request->getParam('_csrfToken')); ?>

<?= json_encode($this->request->getAttribute('csrfToken')); ?>

另外,我不确定您的“beforeSend:function(xhr)",您可以在这里看到一个工作的 AJAX 调用示例:
https://stackoverflow.com/a/67253585/15256337

相关问题