Laravel 5,阿贾克斯,500内部服务器错误,TokenMismatchException在VerifyCsrfToken.php 46行: [英] Laravel 5, ajax, 500 Internal Server Error, TokenMismatchException in VerifyCsrfToken.php line 46:

查看:376
本文介绍了Laravel 5,阿贾克斯,500内部服务器错误,TokenMismatchException在VerifyCsrfToken.php 46行:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel 5在弹出警报阿贾克斯后,它给我的错误500内部服务器错误,当我检查萤火虫,我发现AJAX返回错误页面说:TokenMismatchException在VerifyCsrfToken.php第46行:

当我评论程序\ HTTP \中间件\ VerifyCsrfToken ,它工作正常,但我认为它不推荐,我打印_token变量,它不是空的
阿贾克斯后code是:

  $('#demo_4)。点击(函数(){
        bootbox.prompt(你叫什么名字?,函数(结果){
            如果(结果=== NULL){
                警报(提示驳回);
            } 其他 {
                //警报(你好< B>中+结果+< / B>中);
                $阿贾克斯({
                    网址:'测试',
                    类型:后,
                    数据:{名称:因此,_token'。$('输入[名称= _token])VAL()},
                    成功:功能(数据){
                        警报(数据);
                    }
                });
            }
        });
    });
 

路径code是:

 路线::职位('测试','的AccountController @登录');
 

在的AccountController code是:

 < PHP
命名空间的应用程序\ HTTP \控制器;
使用输入;
使用要求;
使用应用程序\ HTTP \控制器;

一流的AccountController扩展控制器
{

    公共职能的登录()
    {

        如果(支持::阿贾克斯()){
            $数据=输入::所有();
            的print_r($的数据);
            死;
        }

    }
}
 

解决方案

您需要将此行插入到表单

 <输入类型=隐藏名称=_标记值=< PHP的回声csrf_token();?>>
 

或添加这控制器

 使用照亮的\ Support \外立面\会议
 

请启用调试,并提供什么Laravel5调试器显示讲更多的细节。

在AJAX调用它添加到数据选项:

 '_令牌:$('输入[名称= _token])VAL()。
 

还是在全球范围内的方法添加

 < META NAME =_标记内容={!csrf_token()!}/>
 

和添加到页脚:

 <脚本类型=文/ JavaScript的>
    $ .ajaxSetup({
        标题:{X-CSRF令牌:$('元[名称= _token]')ATTR(内容)。}
    });
< / SCRIPT>
 

UPD:到控制器

 使用输入;
使用要求;
一流的AccountController扩展控制器{
    公共职能的登录()
    {
    //获取所有数据后
    如果(支持::阿贾克斯()){
        $数据=输入::所有();
        的print_r($的数据);模具;
    }
}
 

UPD2:尝试将它添加到您的主要布局

 < META NAME =_标记内容={!csrf_token()!}/>

<脚本类型=文/ JavaScript的>
    $ .ajaxSetup({
        标题:{X-CSRF令牌:$('元[名称= _token]')ATTR(内容)。}
    });
< / SCRIPT>
 

请检查你的web服务器已经写访问会话目录,也可以是应用程序/存储/框架/会话/ 。 执行

 的rm -f {} your_web_app /存储/框架/会话/ *
 

刷新的Web服务器。

在某些情况下,检查应用程序/ HTTP /中间件/ VerifyCsrfToken.php tokensMatch 方法与此code:

  $令牌= $请求 - > AJAX()? $请求 - >标题(X-CSRF令牌'):$请求 - >输入('_令牌');

返回$请求 - >会话() - >令牌()== $令牌;
 

和检查JavaScript文件这样的:

  // CSRF保护
$ .ajaxSetup(
{
    标题:
    {
        X-CSRF令牌:$('输入[名称=_标记])VAL()。
    }
});
 

I'm using Laravel 5 with ajax post in a popup alert, and it giving me the error "500 Internal Server Error", when I checked firebug, I find that ajax return an error page saying "TokenMismatchException in VerifyCsrfToken.php line 46:"

when I comment App\Http\Middleware\VerifyCsrfToken, it works fine, but I assume that it's not recommended, I print the _token variable and it's not empty
the ajax post code is:

 $('#demo_4').click(function(){
        bootbox.prompt("What is your name?", function(result) {
            if (result === null) {
                alert("Prompt dismissed");
            } else {
                // alert("Hi <b>"+result+"</b>");
                $.ajax({
                    url: 'test',
                    type: "post",
                    data: {'name':result, '_token': $('input[name=_token]').val()},
                    success: function(data){
                        alert(data);
                    }
                });
            }
        });
    });

the Route code is:

Route::post('test', 'AccountController@login');

the AccountController code is:

<?php
namespace App\Http\Controllers;
use Input;
use Request;
use App\Http\Controllers;

class AccountController extends Controller 
{

    public function login()
    {

        if (Request::ajax()) {
            $data = Input::all();
            print_r($data);
            die;
        }

    }
}

解决方案

You need to insert this line to your form

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

Or adding this to controller

use Illuminate\Support\Facades\Session

Please enable debugger and provide what Laravel5 debugger shows to speak more detail.

In ajax call add this to data option:

'_token':$('input[name=_token]').val()

Or in globally way add

<meta name="_token" content="{!! csrf_token() !!}"/>

And add to footer:

<script type="text/javascript">
    $.ajaxSetup({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
    });
</script>

UPD: to controller

use Input;
use Request;
class AccountController extends Controller {
    public function login() 
    {
    // Getting all post data
    if(Request::ajax()) {
        $data = Input::all();
        print_r($data);die;
    }
}

UPD2: try to add this to your main layout

<meta name="_token" content="{!! csrf_token() !!}"/>

<script type="text/javascript">
    $.ajaxSetup({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
    });
</script>

Check that your web server has already write access to session directory, it can be app/storage/framework/sessions/. Execute

rm -f {your_web_app}/storage/framework/sessions/*

Reload web server.

In some cases check app/Http/Middleware/VerifyCsrfToken.php for tokensMatch method with this code:

$token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');

return $request->session()->token() == $token;

And check javascript file for this:

// CSRF protection
$.ajaxSetup(
{
    headers:
    {
        'X-CSRF-Token': $('input[name="_token"]').val()
    }
});

这篇关于Laravel 5,阿贾克斯,500内部服务器错误,TokenMismatchException在VerifyCsrfToken.php 46行:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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