代码点火器CSRF Jquery问题


Codeigniter CSRF Jquery Issue

我很困惑为什么codeigniter不让我使用这个:

$(function() {
    var csrf = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>"),
        csrfToken = "<?php echo $this->security->get_csrf_token_name(); ?>";
    $('.notificationBoxClose').click(function() {
        var url = $(this).attr('href');
        $.post(url, {csrfToken: csrf}, function() {
            $('#notification').fadeOut('slow', function() {$this.remove()});
        });
        return false;
    });
});

当我做同样的代码时,它工作得很好:

$(function() {
    var csrf = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>");
    $('.notificationBoxClose').click(function() {
        var url = $(this).attr('href');
        $.post(url, {<?php echo $this->security->get_csrf_token_name(); ?>: csrf}, function() {
            $('#notification').fadeOut('slow', function() {$this.remove()});
        });
        return false;
    });
});

为什么我不能使安全性->get_csrf_token_name();?>转换为变量?

因为不能将变量用作对象键。你需要像这样插入:

$(function() {
    var postData = {};
    var csrfToken = "<?php echo $this->security->get_csrf_token_name(); ?>";
    postData[csrfToken] = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>"),
    $('.notificationBoxClose').click(function() {
        var url = $(this).attr('href');
        $.post(url, postData, function() {
            $('#notification').fadeOut('slow', function() {$this.remove()});
        });
        return false;
    });
});