我正在处理单页应用程序,我正在使用Laravel 5进行Web服务.
所有表单都是异步提交的,我在它们上面使用了一个beforeSend来附加我从meta标签中获取的CSRF令牌,如下所示:
$.ajax({
url: '/whatever/route',
type: 'POST',
dataType: 'JSON',
data: $('form#whatever-form').serialize(),
beforeSend: function(request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='token']").attr('content'));
},
success: function(response){
rivets.bind($('#whateverTag'), {whateverData: response});
},
error: function(response){
}
});
Run Code Online (Sandbox Code Playgroud)
我的所有表单都运行正常但dropzone上传却没有.它给了我一个TokenMismatchException例外.这是我的dropzone代码来更新个人资料照片:
$("#mydropzone").dropzone({
url: "/profile/update-photo",
addRemoveLinks : true,
maxFilesize: 5,
dictDefaultMessage: '<span class="text-center"><span class="font-lg visible-xs-block visible-sm-block visible-lg-block"><span class="font-lg"><i class="fa fa-caret-right text-danger"></i> Drop files <span class="font-xs">to upload</span></span><span>  <h4 class="display-inline"> (Or Click)</h4></span>',
dictResponseError: 'Error uploading file!'
});
Run Code Online (Sandbox Code Playgroud)
我也试过把它beforeSend放在这里:
$("#mydropzone").dropzone({
url: "/profile/update-photo",
addRemoveLinks : true,
maxFilesize: 5,
dictDefaultMessage: '<span class="text-center"><span class="font-lg visible-xs-block …Run Code Online (Sandbox Code Playgroud) 我有一个包含表单的vue模板:
<form id="logout-form" :action="href" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Run Code Online (Sandbox Code Playgroud)
在laravel中,表单必须定义csrf_field().但是在一个vue组件中,该语句{{ csrf_field() }}意味着我有一个csrf_field在我的vue实例中命名的方法,而我正在调用它.
在这种情况下如何添加csrf_field?