我使用Codeigniter 3构建了一个简单的项目,当我在localhost中使用$.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';
}
}
}
});
请告诉我如何解决此问题。 这是我的登录控制器
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));
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为您忘记设置.htaccess
配置。
因此,请在项目名称 .htaccess 的根目录中创建一个新文件,并在其下面粘贴代码
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /pishtazan/ # this is for the subfolder in my localhost if you work online remove name of folder
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /pishtazan/index.php [PT,L] #also here you can rename/delete folder name
</IfModule>
如果您不想要它,或者它与您的整个项目冲突,只需将您的javascript ajax网址编辑为'index.php/login/authenticate'
并尝试