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



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

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

这是我用于呈现类SignaturePad的代码:

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());
});

谢谢大家

相关内容

  • 没有找到相关文章

最新更新