【问题标题】:Getting 500 internal server error on ajax post request在 ajax 发布请求上出现 500 内部服务器错误
【发布时间】:2017-01-16 15:35:08
【问题描述】:

我在 ajax 发布请求时收到 500 个内部服务器错误。我在本地主机上测试它,我的路线也是准确的。

我已经为这个调用编写了路由,而不是在 url 中调用控制器名称和方法。

我的代码如下,任何帮助将不胜感激。

$('#personal-info').click(function(){
    //  Var paramstr = $("#personal-info").serialize();
     var form_data = {            //repair
            firstname: $('#firstname').val(),
            lastname: $('#lastname').val(),
            fathername: $('#fathername').val(),
            cnic: $('#cnic').val(),
            gender: $('#gender').val(),
            marital_status: $('#marital_status').val(),
            day_of_birth: $('#day_of_birth').val(),
            month_of_birth: $('#month_of_birth').val(),
            year_of_birth: $('#year_of_birth').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            postal_code: $('#postal_code').val(),
            country_id: $('#country_id').val(),
            state_id: $('#state_id').val(),
            industry_id: $('#industry_id').val(),
            experienc_level: $('#experienc_level').val(),
            current_salary: $('#current_salary').val(),
            expected_salary: $('#expected_salary').val(),
            salary_currency: $('#salary_currency').val(),
            address: $('#address').val(),
            skype: $('#skype').val(),
            professional_summary: $('#professional_summary').val(),
            face_book: $('#face_book').val(),
            twitter: $('#twitter').val(),
            linkedin: $('#linkedin').val(),
            blog: $('#blog').val(),
            website: $('#website').val(),
            // created_at: $('#created_at').val()
        };


    $.ajax({
        url: "<?php echo base_url('add_personal_info'); ?>",//repair
        type: 'POST',
        data: form_data, // $(this).serialize(); you can use this too
        success: function(msg) {
              alert("success..!! or any stupid msg");
        }

   });
   return false;

});

我的 PHP 代码是:

public function add_candidate_personal_info(){

  $candidate = $this->input->post();
  $this->load->model('common_model');
  // $this->load->library('form_validation');
  $this->load->helper('common_helper');
  $this->load->model('User_model');

  $this->form_validation->set_rules('firstname', 'First Name', 'required');
  $this->form_validation->set_rules('lastname', 'Last Name', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[candidate.email]');
  $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
  if (empty($_FILES['userfile']['name']))
  {
      $this->form_validation->set_rules('userfile', 'File Upload', 'required');
  }

  if($this->form_validation->run()==FALSE){
    $formError = validation_errors();
    $this->data['errorMessage'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
    $this->load->view('front/user/candidate/add', $this->data);
   // redirect('register_candidate');
  }
  else {
    $personal_data = array(
        'firstname' => $this->input->post('firstname'),
        'lastname' => $this->input->post('lastname'),
        'father_name' => $this->input->post('father_name'),
        'cnic' => $this->input->post('cnic'),
        'gender' => $this->input->post('gender'),
        'marital_status' => $this->input->post('marital_status'),
        'day_of_birth' => $this->input->post('day_of_birth'),
        'month_of_birth' => $this->input->post('month_of_birth'),
        'year_of_birth' => $this->input->post('year_of_birth'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
        'postal_code' => $this->input->post('postal_code'),
        'country_id' => $this->input->post('country_id'),
        'state_id' => $this->input->post('state_id'),
        'industry_id' => $this->input->post('industry_id'),
        'experienc_level' => $this->input->post('experienc_level'),
        'current_salary' => $this->input->post('current_salary'),
        'expected_salary' => $this->input->post('expected_salary'),
        'salary_currency' => $this->input->post('salary_currency'),
        'address' => $this->input->post('address'),
        'skype' => $this->input->post('skype'),
        'professional_summary' => $this->input->post('professional_summary'),
        'face_book' => $this->input->post('face_book'),
        'twitter' => $this->input->post('twitter'),
        'linkedin' => $this->input->post('linkedin'),
        'blog' => $this->input->post('blog'),
        'website' => $this->input->post('website'),
        'created_at' => date('Y-m-d H:i'),
    );

     if ($this->common_model->insert('candidate_resume_profile', $personal_data)) {
            $this->ajaxResponse['status'] = 200;
            $this->ajaxResponse['message'] = $this->message->success('Candidate Personal Information is Successfully Added');
            $this->ajaxResponse['data'] = '';
            exit(json_encode($this->ajaxResponse));
        } else {
            $this->ajaxResponse['status'] = 500;
            $this->ajaxResponse['message'] = $this->message->error('Internal Error');
            $this->ajaxResponse['data'] = '';
            exit(json_encode($this->ajaxResponse));
        }

  }
}

【问题讨论】:

  • 错误 500 来自您提交的页面,而不是您的 javascript 函数。检查您的 PHP 文件,并检查您的服务器错误日志。 500 是一条非常通用的消息,表示服务器无法处理请求。
  • 你处理这个的 PHP 是什么?
  • 你的文件类型是 .js 还是 .php ?
  • 这是我的 php 代码。

标签: php ajax codeigniter


【解决方案1】:

我认为你可以查看浏览器的信息,或者你可以查看 Nginx/apache 的 error.log 在 Linux 中,您可以使用命令 locate error.log 来查找您的 error.log

【讨论】:

    【解决方案2】:

    这可能是因为您的 php 基础文件权限。 尝试更改权限:

    • 将您的“base_url('add_personal_info')”文件和所有其他基本文件权限更改为 644。
    • 将他们的文件夹权限更改为 755。

    【讨论】:

      最近更新 更多