如何修复“状态错误为500的Ajax错误”?

时间:2019-04-19 12:26:42

标签: php jquery json ajax

我使用ajax jQuery发送输入数据,但是我得到了状态错误代码500,如何解决它以获得状态代码200?该代码可在我的本地主机上运行,​​但是我想托管此代码,但出现此错误,请帮助我修复它

这是我的控制人

private function insert(){
    $hasil = $this->model->insert($_POST['bulan'], $_POST['tahun'], $_POST['petugas']);
    return $hasil;
}

这是我的模型代码

public function insert($bulan, $tahun, $petugas){

    $sql10 = "SELECT bulan_tutupBuku, tahun_tutupBuku FROM tutupBuku;";
    $statement1 = $this->connection->prepare($sql10);
    $statement1->execute();
    $fetch1 = $statement1->fetchAll();
    $statement1->closeCursor();
    if (!empty($fetch1)) {
        foreach ($fetch1 as $fetch2) {
        }
    }
    if ($fetch2['bulan_tutupBuku'] === $bulan && $fetch2['tahun_tutupBuku'] === $tahun) {
        return "error";
    }
    else if(empty($bulan) || empty($tahun) || empty($petugas)){
        return "kosong";
    }else{
        $sql0 = "INSERT INTO tutupBuku(bulan_tutupBuku, tahun_tutupBuku, petugas) VALUES(:bulan, :tahun, :petugas)";
        $statement = $this->connection->prepare($sql0);
        $statement->bindValue(':bulan',$bulan);
        $statement->bindValue(':tahun',$tahun);
        $statement->bindValue(':petugas',$petugas);
        $statement->execute();
        $fetch = $statement->fetchAll();
        $statement->closeCursor();
        return "success";
    } 
}

这是我的Ajax代码

  $("#simpan_user").click(function(){
    var data = {
        aksi    : "insert",
        bulan   : $("select[name='bulan']").val(),
        tahun   : $("input[name='tahun']").val(),
        petugas : "<?php echo $_SESSION['f658f7a22761210065c7ae4211aab09b'];?>"
    };

    console.log(data);
    $.ajax({
      type: "POST",
      url: "index.php?page=6a3b61f42cded56019b264080e226e40&kode=tutupBuku",
      data: data,
      dataType: "json",
      success: function(msg) {
        if (msg === 'success') {
          swal ( "Success" ,  "Berhasil Tutup Buku" ,  "success" );
          location.reload();
        }else if(msg === 'kosong'){
          swal ( "Oops" ,  "Inputan Masih Ada Yang Kosong !" ,  "warning" )
        }else if(msg === 'error'){
          swal ( "Oops" ,  "Buku Sudah Ditutup Pada Bulan dan Tahun Tersebut!" ,  "error" )
        }
      }
    });
  });

0 个答案:

没有答案