繁体   English   中英

Codeigniter不接受启用CSRF保护的jQuery POST

[英]Codeigniter not accepting jQuery POST with CSRF protection enabled

我一直在尝试POST到Codeigniter控制器(启用CSRF保护时),但是由于HTTP/1.1 500 Internal Server Error一直失败,并且This action is not allowed页面。

我尝试使用以下方法(在此处找到)将csrf令牌与POST数据一起发送,但它似乎不起作用。

<script type="text/javascript" src="js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/libs/jquery.cookie.js"></script> 
<div id="display">Loading...</div>
<script type="text/javascript">
   // this bit needs to be loaded on every page where an ajax POST may happen
   $.ajaxSetup({
       data: {
           csrf_token: $.cookie('csrf_cookie')
        }
    });

   // now you can use plain old POST requests like always
   $.post('http://localhost/index.php/handler/test', { name : 'Grim'}, function(data){ $('#display').html(JSON.stringify(data)); });

这是我的Codeigniter config.php文件:

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_token';
$config['csrf_cookie_name'] = 'csrf_cookie';
$config['csrf_expire'] = 1800;

这是控制器:

function test()
{

    echo json_encode($_POST);
}

发布前将csrf令牌添加到数据

$.ajax({
            type: "POST",
            url: url,
            data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>',/*----your data-----*/}

        })

csrf令牌需要随每个请求一起发送,因此需要由上述echo语句指定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM