【发布时间】:2014-08-01 08:27:52
【问题描述】:
请在尝试否决之前阅读此问题。这个问题有很多重复项,不幸的是,没有一个对我有用。
我使用的是 CodeIgniter_2.1.4 版本。
我有一个简单的注册表单,我需要通过 Ajax 请求将其数据插入数据库。当表格没有错误时:
POST path/index.php/controller/method 500 (Internal Server Error)
我尝试了以下解决方案,但仍然出现错误:
- 我在
config.php文件中启用了CSFR 保护:$config['csrf_protection'] = TRUE; - 我正在使用
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