简体   繁体   English

开机自检 <target url> 419(状态未知)-无法在Laravel中发布数据

[英]POST <target url> 419 (unknown status) - can't post data in Laravel

I'm trying to make a DOM event where the user clicking a table row's header ( th ) cell will delete the corresponding row from the database that supplies the table's data. 我正在尝试创建一个DOM事件,其中用户单击表行的标题( th )单元格将从提供表数据的数据库中删除相应的行。

This code worked as intended framework-less, by just POST'ing an AJAX containing the row's id info from index.php to delete.php which then ran a sql query. 通过仅将包含该行的ID信息的AJAX从index.php POST到delete.php,然后运行sql查询,此代码就可以在没有框架的情况下工作了。

However, after moving the site to Laravel I ran into an error: 但是,将站点移至Laravel之后,我遇到了一个错误:

POST http://sandbox.app/delete 419 (unknown status)

The JavaScript piece responsible for attaching the delete event and posting the id through AJAX: 负责附加delete事件并通过AJAX发布ID的JavaScript代码:

    function attachDelete() {
        $("#mainTable tbody tr th").on("click", function(e){
            console.log(e.target.innerText + " was clicked");
            var token = $('meta[name="csrf-token"]').attr('content');
            var id_to_delete = e.target.innerText;
            $.ajax({
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                type: 'POST',
                dataType: 'text',
                url: 'delete',
                data: { 
                    'id_to_delete': id_to_delete,
                    "_method": 'POST',
                    "_token": token 
                },
                success: function () {alert("Deleted!"); },
                failure: function() {alert("Error!");}
            });
        });
    }
    attachDelete();

The console.log(e.target.innerText + " was clicked"); console.log(e.target.innerText + " was clicked"); goes off. 熄灭。

However, the success / error messages do not appear. 但是,不会显示success / error消息。

Going to http://sandbox.app/delete directly brings up a Laravel error window with lots of text, and this part highlighted: 直接转到http://sandbox.app/delete会打开一个Laravel错误窗口,其中包含很多文本,该部分突出显示:

 protected function methodNotAllowed(array $others)
    {
        throw new MethodNotAllowedHttpException($others);
    }

I've added the token variables after reading answers to related questions on StackOverflow. 在阅读完StackOverflow相关问题的答案后,我添加了令牌变量。 This didn't help. 这没有帮助。

In case it matters, the route: 如果重要,路线:

Route::post('/delete', 'TasksController@delete');

The controller : 控制器:

class TaskController extends Controller
{
    public function delete() 
    {
        include 'config.php';

        $stmt = $pdo->prepare('DELETE FROM food WHERE id = :id');
        $stmt->execute(['id' => $_POST['id_to_delete']]);
    }

}

The code inside delete() used to just be the contents of a delete.php file in the old, framework-less site, where everything worked. delete()内的代码过去只是旧的无框架站点中delete.php文件的内容,在该站点中一切正常。

I've tried doing it without the controller by creating a delete.php view (with the same code as the delete() function). 我试图通过创建delete.php视图(使用与delete()函数相同的代码)在没有控制器的情况下进行操作。 It didn't make a difference though: 但这并没有什么不同:

Route::post('/delete', function () {
    return view('delete');
});

Well, it seems if you are communicating with POST at the backend. 好吧,看来您是否在后端与POST通信。 So, you should be configuring your routes on the api.php instead of web.php 因此,您应该在api.php而不是web.php上配置路由

Place your route associated with controller 放置与控制器关联的路线

Route::post('/delete', 'TasksController@delete');

Inside api.php file. 内部api.php文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM