2012-04-09 69 views
1

当用户点击下拉菜单并做出不同的选择,然后更改名为defaultCharacterID的会话密钥时,会运行此脚本,然后我会计算出何时在控制台中显示发布请求它说:“您所请求的操作是不允许的下拉式jQuery没有执行

PHP:

public function changeDefaultCharacter() 
{ 
    if ($this->input->post('defaultCharacterID')) 
    { 
     $this->session->set_userdata($this->input->post('defaultCharacterID')); 
    } 
} 

的jQuery:

$(document).ready(function() { 
    $('#charactersDrop').change(function() { 
    // POST the changed value to a method in a controller that can accept 
    // a parameter and that'll set the session variable for you 
     $.post('dashboard/changeDefaultCharacter', 
      { defaultCharacterID: this.value }, 
      'html' 
     ); 
    }); 
}); 

编辑:

$(document).ready(function() { 
$('#charactersDrop').change(function() { 
    // POST the changed value to a method in a controller that can accept 
    // a parameter and that'll set the session variable for you 
    $.post('dashboard/changeDefaultCharacter', 
     { defaultRosterListID: this.value }, 
     <?php echo $this->security->get_csrf_token_name(). ':'. 
      $this->security->get_csrf_hash(); ?> 
     } 
    , 
    'html' 
}); 
}); 
+0

你使用什么版本的CI的? – safarov 2012-04-09 13:31:58

+0

版本2.1我相信 – 2012-04-09 13:35:39

+0

检查配置文件的'csrf_protection'值,确保它的'false' – safarov 2012-04-09 13:36:25

回答

0

变化config.php并确保:

$config['csrf_protection'] = FALSE; 

此错误的原因是因为阿贾克斯的保护,检查会话令牌。您也可以将其取消临时只有一个控制器

$this->config->set_item("csrf_protection",FALSE); 

修订

或者更好的休假保护true并添加csrf token您的要求

$.post('dashboard/changeDefaultCharacter', 
     { 
     defaultCharacterID: this.value, 
     <?php echo $this->security->get_csrf_token_name(). ':'. 
       $this->security->get_csrf_hash(); 
     ?> 
     } 
     , 
     'html' 
    ); 
    ... 
+0

暗示禁用安全性听起来不是对我的好方法。这就像建议如果锁具有问题需要移除门 – mkk 2012-04-09 13:48:30

+0

好吧,现在我有控制器功能是否正确。有了这样的代码,它会代替旧的价值吗? – 2012-04-09 13:49:07

+0

所以你认为它应该工作,因为我不知道如何测试它是否真的改变了。 – 2012-04-09 13:55:39