Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 错误:POSThttp://localhost:8765/users/delete/delete8 403(禁止)_Javascript_Php_Jquery_Cakephp_Cakephp 4.x - Fatal编程技术网

Javascript 错误:POSThttp://localhost:8765/users/delete/delete8 403(禁止)

Javascript 错误:POSThttp://localhost:8765/users/delete/delete8 403(禁止),javascript,php,jquery,cakephp,cakephp-4.x,Javascript,Php,Jquery,Cakephp,Cakephp 4.x,我用的是cakephp4。正在尝试添加一些提示。当单击“删除”按钮时,会弹出Sweetalert,但当我单击“确认删除”时,它不会删除数据 模板/layout/Users/index.php <table> <tr> <th>Username</th> <th>Usertype</th> <th>Created</th> <th>Action</th&

我用的是cakephp4。正在尝试添加一些提示。当单击“删除”按钮时,会弹出Sweetalert,但当我单击“确认删除”时,它不会删除数据

模板/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; ?>
<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);
            }
          })
       
      } 
    })
  })
})
路由文件在这里 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();

我确信这是CSRF令牌。试着替换

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


另外,我也不确定您的“beforeSend:function(xhr)”,您可以在这里看到一个工作的ajax调用示例:


如果错误与标题中的错误相同,则您很可能在id前面缺少斜杠字符
/
。请注意您所指的URL。也许您可以在发送请求之前预览其中的内容?尝试将
console.log('/users/delete/'+ajax\u url)
放在
Swal.fire
行之前。我做了console.log('/users/delete/'+ajax\u url)。在这里,我得到了这个URL/users/delete/username用户名,不管用户名是什么,你可以共享路由吗?路由什么?应用程序路由。如果您使用的是Cake框架,那么可能有一个
config/routes.php
文件。我想看看这个端点的定义是什么。
<?= json_encode($this->request->getParam('_csrfToken')); ?>
<?= json_encode($this->request->getAttribute('csrfToken')); ?>