从 canvas 生成数据时出现 CSRF 令牌错误

CSRF token error when generating data from canvas

我正在使用一个名为 laravel-admin 的软件包。

我用流行的 SignaturePad 包创建了一个自定义签名字段,数据成功转换为图像。但是,当我尝试提交表单时,我收到一条错误消息,提示 CSRF 令牌未匹配。

这是我的渲染代码 SignaturePad class:

public function render()
{
    $this->script = <<<EOT
    var canvas = document.getElementById('signature-pad');

    // Adjust canvas coordinate space taking into account pixel ratio,
    // to make it look crisp on mobile devices.
    // This also causes canvas to be cleared.
    function resizeCanvas() {
        // When zoomed out to less than 100%, for some very strange reason,
        // some browsers report devicePixelRatio as less than 1
        // and only part of the canvas is cleared then.
        var ratio = Math.max(window.devicePixelRatio || 1, 1);
        canvas.width = canvas.offsetWidth * ratio;
        canvas.height = canvas.offsetHeight * ratio;
        canvas.getContext("2d").scale(ratio, ratio);
    }

    window.onresize = resizeCanvas;
    resizeCanvas();

    var signaturePad = new SignaturePad(canvas, {
        backgroundColor: 'rgb(255, 255, 255)' // necessary for saving image as JPEG; can be removed is only saving as PNG or SVG
    });

    $('button[type="submit"]').click(function() {
        console.log(signaturePad.toDataURL());
        $('input[type=hidden]').val(signaturePad.toDataURL());
    });

    EOT;
    return parent::render();
}

如果我删除下面的行,那么我就不会收到它,但显然我的数据没有发送。它的工作方式是我转换图像的值并将其放入要发送的隐藏输入字段。有人可以帮忙吗?

$('button[type="submit"]').click(function() {
    console.log(signaturePad.toDataURL());
    $('input[type=hidden]').val(signaturePad.toDataURL());
});

显然我忘记了 laravel 提供了包含我正在更改的 CSRF 令牌的隐藏输入以及 jQuery 代码

$('button[type="submit"]').click(function() {
    console.log(signaturePad.toDataURL());
    $('input[type=hidden]').val(signaturePad.toDataURL());
});

谢谢大家