如何将CSRF字段添加到使用jquery生成的表单中(Laravel 5) [英] How to add the CSRF field to a form generated with jquery.(Laravel 5)

查看:57
本文介绍了如何将CSRF字段添加到使用jquery生成的表单中(Laravel 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel要求您这样编写表单.

Laravel requires you to write your form like this.

<form method ="POST" action="whatever">
    {{ csrf_field() }} 
</form>

我已经使用以下代码在网站中创建了约15种带有jquery的表单.

I've already created around 15 forms with jquery in my website with code like the following.

$("#formholder).append("<form id='myForm' method='post' action ='whatever'></form>");

我的问题是:

如何在使用jquery创建的这些表单上添加csrf字段?

How do I add the csrf field on these forms created with jquery?

推荐答案

将此meta标签与其他meta标签一起放入主Blade模板中:

Put this meta tag into the main Blade template with other meta tags:

<meta name="csrf-token" content="{{ csrf_token() }}">

然后您就可以通过JS访问它了

Then you'll be able to access it with this from JS:

$('meta[name="csrf-token"]').attr('content')

https://laravel.com/docs/5.5/csrf #csrf-x-csrf-token

这篇关于如何将CSRF字段添加到使用jquery生成的表单中(Laravel 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆