【问题标题】:Why am I still getting 500 internal server error in Codeigniter?为什么我仍然在 Codeigniter 中收到 500 内部服务器错误?
【发布时间】:2014-08-01 08:27:52
【问题描述】:

请在尝试否决之前阅读此问题。这个问题有很多重复项,不幸的是,没有一个对我有用。

我使用的是 CodeIgniter_2.1.4 版本。

我有一个简单的注册表单,我需要通过 Ajax 请求将其数据插入数据库。当表格没有错误时:

POST path/index.php/controller/method 500 (Internal Server Error)

我尝试了以下解决方案,但仍然出现错误:

  1. 我在config.php 文件中启用了CSFR 保护:$config['csrf_protection'] = TRUE;
  2. 我正在使用form_open()form_close() 标签,因为CI 文档说如果使用form_open(),它将自动在表单中插入隐藏的csrf 字段。

Javascript

$("#sign-up").submit(function(e){
    e.preventDefault();                  


    alert($(this).serialize());
    $.ajax({
        method:"POST",
        data:$(this).serialize(),
        url:"<?php echo site_url('viewer/signup_process')?>",
        success:function(data){
            $("#update").html(data);
        }
    }) ;
});

PHP 代码

public function signup_process()
{
    $this->form_validation->set_rules("firstname","Firstname","required");
    $this->form_validation->set_rules("lastname","Lastname","required");
    $this->form_validation->set_rules("username","Username","required|max-length[10]");
    $this->form_validation->set_rules("password","Password","required|min-length[5]");
    $this->form_validation->set_rules("telephone","Telephone","required");
    $this->form_validation->set_rules("email","email","required|valid_email");
    $this->form_validation->set_rules("terms","Terms & Conditions","callback_agree_to_terms");

    if(!$this->form_validation->run()){
        //There are errors
        $data["title"]="Errors";
        $data["errors"]=  validation_errors();            
        $this->load->view("public/errors",$data);
    } else {
        $viewer=array(
            "firstname"=>  $this->input->post("firstname"),
            "lastname"=>  $this->input->post("lastname"),
            "username"=>  $this->input->post("username"),
            "password"=> $this->encrypt->sha1($this->input->post("password")),
            "telephone"=>  $this->input->post("telephone"),
            "email"=>  $this->input->post("email")
        );
        $create_result=  $this->Viewer_model->create_viewer($viewer);

        if($create_result){
            $data["message"]="Registration successful..Please sing in";
            $this->load->view("public/success",$data);
        }
    }
}

这可能是什么原因?

【问题讨论】:

  • 如果您向自己的网站发出请求,请调高您的error_reporting 级别并检查您的日志以查看实际错误是什么?
  • 如果您只是在本地进行测试,请尝试 try catch 并回显您遇到的错误。
  • @sevenseacat - 我在我的 php.ini 文件中创建了 error_reporting = E_ALL
  • @Jimmy - 我在 try catch 块中尝试了我的 create 语句,但在 catch 块中没有打印异常。是的,我正在本地测试它
  • 你的前端和后端在不同的服务器上吗?

标签: php ajax codeigniter


【解决方案1】:

尝试改变

method:"POST",

type:"POST",

【讨论】:

  • 对不起。它没有工作
【解决方案2】:

回答我自己的问题

我可以通过使用 htaccess 重写我的索引文件来解决这个问题。

步骤

1) 创建一个.htaccess文件并保存在root目录

这是 .htaccess 文件

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|css|js|img|robots\.txt) 
RewriteRule ^(.*)$ /project_name/index.php/$1 [L]

评论 - 您的 css、js、img 文件夹可能有不同的名称。您可能有更多的文件夹,也可能没有。相应地调整它。

2) 转到 application->config->config.php 并制作

$config['index_page'] = ''; 

就是这样。

希望这对未来的读者有所帮助

【讨论】:

    【解决方案3】:

    您是否在autoload 文件中包含了表单验证模块?

    否则,您需要在函数的第一行执行此操作:

    $this->load->library('form_validation');
    

    【讨论】:

    • 它不在自动加载文件中。但它在我的控制器的构造函数中
    猜你喜欢
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 2021-04-07
    • 2014-09-17
    • 2019-10-25
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多