How to include CSRF from Codeigniter into ajax data

前端 未结 6 2026
傲寒
傲寒 2020-12-10 18:04

I am trying to pass some data into my Controller, but I\'m getting a 500 error. After some research, I discovered that it\'s caused by the CSRF token not being sent.

6条回答
  •  隐瞒了意图╮
    2020-12-10 18:26

    Here's a different approach. Simple function in Auth.php that returns the csrf token name and hash in JSON format. Then, in our javascript, make two ajax calls, the first to grab the csrf creds and insert them into hidden form fields, the second to handle our actual form submit.

    // place function in Auth.php controller

    public function get_csrf()
    {
        $csrf['csrf_name'] = $this->security->get_csrf_token_name();
        $csrf['csrf_token'] = $this->security->get_csrf_hash();
    
        echo json_encode($csrf);
    }
    

    // myFunction()

    
    

    // html



提交回复
热议问题