【发布时间】:2018-10-23 16:03:11
【问题描述】:
我使用 Codeigniter 3 构建了一个简单的项目,当我在 localhost 中使用 $.ajax 方法发送 ajax 请求时,它运行良好,但我得到了
“403 禁止”
当我在实时服务器上这样做时出错。
我在 config.php 中设置了 $config['csrf_protection'] = FALSE; 和 $config['csrf_regenerate'] = FALSE;。
这是使用ajax发送数据的js代码。
$.ajax({
url : '/login/authenticate',
type : 'post',
data : $(this).serialize(),
success : function(response) {
if (response.state == false) {
var msg = response.msg;
err_msg(msg);
} else {
if(response.type == "admin"){
window.location.href ='/admin';
} else {
window.location.href = '/user';
}
}
}
});
请告诉我如何解决这个问题。 这是我的登录控制器
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('login_view');
}
//check the email and password and log the user in if the user info is correct
public function authenticate()
{
$this->load->model("userModel","user", true);
$this->load->library('form_validation');
//Form validation - codeigniter provides you with powerful form validation functionality
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
$res = array('state' => false, 'msg' => validation_errors());
} else {
$type = $this->user->login($email, $password);
if ($type == "user" ) {
$res = array('state' => true, 'type' => $type, 'msg' => 'You are logged in!');
$toast = array('state' => true, 'msg' => 'You are logged in!');
$this->session->set_flashdata('toast', $toast);
}else if($type == "admin"){
$res = array('state' => true, 'type' => $type, 'msg' => 'You are logged in!');
$toast = array('state' => true, 'msg' => 'You are logged in!');
$this->session->set_flashdata('toast', $toast);
}else if ($type == -3) {
$msg = "You can't be logged in because you are not active at the moment.";
$res = array('state' => false, 'msg' => $msg);
}else if ($type == -1) {
$msg = "Wrong Password!";
$res = array('state' => false, 'msg' => $msg);
}else {
$msg = "You were not registered!";
$res = array('state' => false, 'msg' => $msg);
}
}
return $this->output
->set_content_type('application/json')
->set_output(json_encode($res));
}
}
【问题讨论】:
-
嗨,还是不行
-
注释掉 $this->session->set_flashdata 行时出现同样的问题
标签: ajax codeigniter-3