我想为帖子创建一个类似按钮,但是当我尝试它时,我收到此错误:
TokenMismatchException in VerifyCsrfToken.php line 46:
in VerifyCsrfToken.php line 46
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\wamp\www\forum\public\index.php') in server.php line 21
这是我的表格:
<form method="POST" id="post_up" action="">
<input type="hidden" name="post" value="{{ $post->id }}" />
<input type="hidden" name="user" value="{{ Auth::user()->id }}" />
<button type="submit" class="btn btn-default" aria-label="Left Align">
<span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span>
</button>
</form>
jQuery代码:
<script type="text/javascript" src="{{ asset('js/jquery-2.2.1.js')}}"></script>
<script type="text/javascript">
$(function(){
$(#errors_).hide();
$(#post_up).submit(function(e){
e.preventDefault();
var post = $('input[name="post"]').val();
var user = $('input[name="user"]').val();
var data = new FormData();
data.append('user',user);
data.append('post',post);
$.ajax({
url:'post/post_vote_up',
type:'POST',
data:data,
contentType:"multipart/form-data",
processData:false,
success:function(data){alert('Section created :)')},
error:function(data){
$(#errors_).show();
$(#errors_).html('');
var errors = data.responseJSON;
$.each(errors,function(k,v){
$(#errors_).append(v+'<br>');
})
}
});
}
})
})
</script>
控制器功能:
public function post_up(Request $r)
{
$p_id = $r->input('post');
$u_id = $r->input('user');
$post = new \App\Post_vote;
$post->post_id=$p_id;
$post->user_id=$u_id;
$post->vote=1;
$post->save();
}
通过此功能路由:
路线::交(&#39; /后/ post_vote_up&#39;,&#39; PostController中@ post_up&#39);
答案 0 :(得分:1)
在表单标记后写下:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
希望这能帮到你.. !!
答案 1 :(得分:0)
这是某种中间件。我猜您应该根据您的请求发送 CSRF-token 。如果您已登录,那么它应该在您的cookie中的某个位置。尝试找到它,然后在 beforeSend 函数中将其作为标题添加到您的请求中。我想这取决于你在后端使用的框架。这就是我为我的Django应用程序做的事情
$ .ajax({
beforeSend:function(xhr, settings){
a=document.cookie.substr(document.cookie.indexOf('csrftoken'));
if (a.indexOf('=') != -1)a = a.substr(a.indexOf('=') + 1);
xhr.setRequestHeader("X-CSRFToken", a);},