Laravel:通过Ajax问题解决CSRF令牌: [英] Laravel: workaround the CSRF token via Ajax issue :

查看:545
本文介绍了Laravel:通过Ajax问题解决CSRF令牌:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我想通过ajax将选定的单词发送到Controller,但是我一直在收到内部服务器错误".在整个星期天的挣扎和宣誓之后,我想我知道为什么会这样以及如何解决.如果我通过普通的表单"和提交"按钮发送单词,那么我就没有这个问题.问题是Ajax与CSRF令牌不匹配之间的误婚.

I want via ajax send chosen words to a Controller, but I am getting all the time "Internal server error" After a full Sunday of struggling and swearing I think I know why this is happening and how it could be solved. I dont have that problem if I send the word via a ordinary Form and Submit button. The issue is the mis-marriage between Ajax and the CSRF token mismatch.

这是Ajax片段>

<script>    
    $(document).ready(function(){           
            $('.choose-language').on('click', function(e){
            e.preventDefault();
            var selectedlanguage = $(this).data('value');
            alert(selectedlanguage); // it gets the value alright on clicking the paragraph
            $.ajax({  // so I want to send it to the controller
            type:"POST",  // via post 
            url: 'language',  // correct?
            data:{'locale': selectedlanguage},

            });   // HERE FINISHES THE $.POST STUFF           

        }); //HERE FINISHES THE CLICK FUNCTION       

    }); // HERE FINISHES THE DOCUMENT AND READY STUFF
    </script>    

这是HTML

<div class="choose-language">
<p class="choose-language" id="english" data-value="en" >English</p>
<p class="choose-language" id="spanish" data-value="es" >Spanish</p>
</div>

这是路线:

Route::get('/', function () {
    return view('welcome');
});

Route::post('language', array(

   'as'   =>'language',
   'uses' => 'LanguageController@changelanguage'

));

还有控制器

class LanguageController extends Controller

{

     public function changelanguage()
    {
        Session::set('locale', \Input::get('locale'));              
        return \Redirect::back();

    }

}

因此,如果我转到中间件,则可以看到有一个名为VerifyCSRFToken.php的文件,并且在该文件中有以下文件:

So, if I go to Middleware, I can see there is a File called VerifyCSRFToken.php and inside that file there is this:

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        // code here
    ];
}

所以,我敢肯定,应该解决它,但是我在这里//的代码写了'language',并且没有任何区别.肯定还有其他错误.

So, I am sure that should fix it, but I wrote 'language' where the // code here is and did not make any difference. There must be other bugs..

非常感谢.

更新:

我发现了一个拼写错误(抱歉,我写的是redirecto而不是redirect),而且我不再遇到错误.

I have found a typo (apologies I had written redirecto instead of redirect) and I m not getting errors anymore.

推荐答案

将CSRF令牌添加到您的HTML头中:

Add the CSRF token to your HTML head:

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

将此添加到您的JS文件中:

Add this to your JS file:

$.ajaxSetup({
    headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
    }
});

CSRF现在应该通过中间件

The CSRF should now pass the middleware

这篇关于Laravel:通过Ajax问题解决CSRF令牌:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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