【问题标题】:Ajax With Csrf Protection showing 403 forbidden in codeigniter 3.1.9带有 Csrf 保护的 Ajax 在 codeigniter 3.1.9 中显示 403 被禁止
【发布时间】:2018-09-14 18:15:12
【问题描述】:

我在通过 ajax 向控制器发送请求并启用 csrf 保护时遇到问题。它总是给出一个错误,因为 403 被禁止。在本地主机和实时服务器上也面临这个问题。使用condeigniter 3.1.9

以下是我的配置值。

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = FALSE;
$config['csrf_exclude_uris'] = array();

Ajax 代码跟随

$.ajax({
    type:"POST",
    url:"http://localhost/Demo/products/getdata",
    dataType:'html',
    data:"char="+char,
    success: function(data){
        $('#proCatS').html(data);
    }
});

请帮助解决这个问题。谢谢!

【问题讨论】:

  • 您需要在您的data 对象中包含令牌,例如var csrfName = '<?php echo $this->security->get_csrf_token_name(); ?>', csrfHash = '<?php echo $this->security->get_csrf_hash(); ?>';
  • 你也可以使用这个例子johnkieken.com/…

标签: ajax codeigniter codeigniter-3


【解决方案1】:

您可以获取 csrf_token_name 和 csrf_hash 代码并在 POST 中传递这些参数

var csfrData = {};
 csfrData['<?php echo $this->security->get_csrf_token_name(); ?>']
                   = '<?php echo $this->security->get_csrf_hash(); ?>';
$.ajax({
    type:"POST",
    url:"http://localhost/Demo/products/getdata",
    data:{char:char, csrf_test_name:csfrData},
    success: function(data){
        $('#proCatS').html(data);
    }
});

【讨论】:

  • 嗨,请再试一次,同时删除数据类型
  • 嗨。仍然在删除数据类型后显示 403 禁止错误。我需要在配置文件中设置任何参数吗
猜你喜欢
  • 2017-05-15
  • 2018-08-12
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2023-03-08
  • 2020-07-07
  • 2021-04-04
相关资源
最近更新 更多