1

当用户单击下拉菜单并进行不同的选择然后更改名为 defaultCharacterID 的会话密钥时,将运行此脚本,我很想弄清楚它何时在控制台中显示“您拥有的操作”的发布请求不允许请求。

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'
});
});
4

1 回答 1

0

更改config.php并确保:

   $config['csrf_protection'] = FALSE; 

由于 ajax 保护,检查会话令牌而导致此错误。您也可以只为一个控制器临时设置它

  $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'
      );
   ...
于 2012-04-09T13:45:06.470 回答