【问题标题】:Codeigniter 403 Forbidden Error when send the data through ajax通过ajax发送数据时Codeigniter 403禁止错误
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我想你忘了设置.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 url 编辑为 'index.php/login/authenticate' 并尝试一下

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 2011-06-24
      • 1970-01-01
      • 2017-05-01
      • 2018-11-03
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多