gpt4 book ai didi

ajax - 通过ajax发送数据时Codeigniter 403 Forbidden Error

转载 作者:行者123 更新时间:2023-12-05 01:05:37 25 4
gpt4 key购买 nike

我已经使用 Codeigniter 3 构建了一个简单的项目,当我在本地主机中使用 $.ajax 方法发送 ajax 请求时,它运行良好,但我得到了

"403 Forbidden"

当我在实时服务器上这样做时出错。我在 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';
}
}
}
});

请告诉我如何解决这个问题。这是我的登录 Controller

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

最佳答案

你好,我在使用上面的代码时没有遇到任何错误......见下图

enter image description here

使用表单名称或表单 ID 来序列化数据而不是 $(this).serialize()

关于ajax - 通过ajax发送数据时Codeigniter 403 Forbidden Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com