Php Codeigniter CSRF Jquery问题

Php Codeigniter CSRF Jquery问题,php,jquery,codeigniter,Php,Jquery,Codeigniter,我不明白为什么codeigniter不让我使用这个: $(function() { var csrf = $.cookie("<?php echo $this->config->item('csrf_cookie_name'); ?>"), csrfToken = "<?php echo $this->security->get_csrf_token_name(); ?>"; $('.notificationBoxC

我不明白为什么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;
    });
});
$(函数(){
var csrf=$.cookie(“”),
csrfToken=“”;
$('.notificationBoxClose')。单击(函数(){
var url=$(this.attr('href');
$.post(url,{csrfToken:csrf},function(){
$('#notification').fadeOut('slow',function(){$this.remove()});
});
返回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;
    });
});
$(函数(){
var csrf=$.cookie(“”);
$('.notificationBoxClose')。单击(函数(){
var url=$(this.attr('href');
$.post(url,{:csrf},函数(){
$('#notification').fadeOut('slow',function(){$this.remove()});
});
返回false;
});
});

为什么我不能进行安全->获取\u csrf\u令牌\u 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;
    });
});
$(函数(){
var postData={};
var csrfToken=“”;
postData[csrfToken]=美元.cookie(“”),
$('.notificationBoxClose')。单击(函数(){
var url=$(this.attr('href');
$.post(url、postData、函数(){
$('#notification').fadeOut('slow',function(){$this.remove()});
});
返回false;
});
});

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

$(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;
    });
});
$(函数(){
var postData={};
var csrfToken=“”;
postData[csrfToken]=美元.cookie(“”),
$('.notificationBoxClose')。单击(函数(){
var url=$(this.attr('href');
$.post(url、postData、函数(){
$('#notification').fadeOut('slow',function(){$this.remove()});
});
返回false;
});
});

有哪些错误?令牌名称是否没有可能破坏javascript代码的字符?在第一个示例中,您没有定义
csrfToken
变量。例如'var csrfToken=“…”。可能这就是错误是“您请求的操作不允许”的原因。错误是什么?令牌名称中没有可能破坏javascript代码的字符吗?在第一个示例中,您没有定义
csrfToken
变量。例如'var csrfToken=“…`。也许这就是为什么错误是“您请求的操作是不允许的”。谢谢,这很有效,但我还有一个问题,如果我还有另外两个函数,我希望能够调用相同的信息,但它们有更多的数据正在推送。有没有办法让它像
$一样使用。post(url,{username:username,email:email,password:password,postData},function(data){
我想使用csrt信息作为常量
$。post(url,$.extend(postData,userData),function(data){})
有关函数$的更多信息。extend:所以我必须这样做:
var userData={};userData[username]=用户名;userData[email]=电子邮件;userData[password]=密码;
无需这样做。你唯一不能做的就是使用变量作为键。
var userData={username:username,email:email,password:password}
将起作用,因为这就像编写
var userData={“username”:username,“email”:email,“password”:password}
谢谢,效果很好,但如果我还有另外两个函数,我想调用相同的信息,但它们有更多的数据,我还有另一个问题。有没有办法让我可以像
$一样使用它。post(url,{username:username,email:email,password:password,postData},function(data){
我想使用csrt信息作为常量
$.post(url,$.extend(postData,userData),function(data){};
关于函数$.extend的更多信息。extend:所以我必须做:
var userData={};userData[username]=用户名;userData[email]=电子邮件;userData[password]=密码;
不需要。唯一不能做的是使用变量作为键。
var userData={username:username,email:email,password:password};
将起作用,因为这就像编写
var userData={“username”:username,“email”:email,“password”:password};