Welcome Guest, Not a member yet? Register   Sign In
Error 'Can't find a route for GET' on Ajax POST
#1

Every time I submit a form, I get an error Can't find a route for 'get: produk/tambahProduk'.
I have set up routes and ajax with post, why I get that error?

Route.php:
Code:
<?php

use CodeIgniter\Router\RouteCollection;

/**
* @var RouteCollection $routes
*/
$routes->get('/', 'home');
$routes->get('home', 'home');
$routes->get('produk', 'Produk::index');
$routes->get('produk/getProduk', 'Produk::getProduk');
$routes->get('instansi/getInstansi', 'Instansi::getInstansi');
$routes->get('produk/getProdukAll', 'Produk::getProdukAll');
$routes->get('produk/getProdukJenis', 'Produk::getProdukJenis');
$routes->post('produk/tambahProduk', 'Produk::tambahProduk');
// $routes->match(['get', 'post'], 'instansi/getInstansi', 'Instansi::getInstansi');

service('auth')->routes($routes);

AJAX:
Code:
$('#formProduk').submit(function (e) {
    e.preventDefault();
    var method = '';
    var id_produk = $("[name=id_produk]").val();

    if (id_produk == '') {
      method = 'tambah';
    } else {
      method = 'ubah';
    }

    $.ajax({
      url: siteurl + "produk/" + method + "Produk",
      type: "post",
      data: $(this).serialize(),
      dataType: 'json',
      beforeSend: function () {

      },
      complete: function () {

      },
      success: function (response) {
        if (response.error) {
          // $.each(response.error, function (key, value) {
          //  // console.log(key);
          // })
         
          if (response.error.tentang) {$('[name="tentang"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.tentang);}
          else{$('[name="tentang"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}

          if (response.error.nomor) {$('[name="nomor"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.nomor);}
          else{$('[name="nomor"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.tahun) {$('[name="tahun"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.tahun);}
          else{$('[name="tahun"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.jenis_id) {$('[name="jenis_id"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.jenis_id);}
          else{$('[name="jenis_id"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.tempat_penetapan) {$('[name="tempat_penetapan"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.tempat_penetapan);}
          else{$('[name="tempat_penetapan"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.tgl_penetapan) {$('[name="tgl_penetapan"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.tgl_penetapan);}
          else{$('[name="tgl_penetapan"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.tgl_undang) {$('[name="tgl_undang"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.tgl_undang);}
          else{$('[name="tgl_undang"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.bidang) {$('[name="bidang"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.bidang);}
          else{$('[name="bidang"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.sumber) {$('[name="sumber"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.sumber);}
          else{$('[name="sumber"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.pemrakarsa) {$('[name="pemrakarsa"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.pemrakarsa);}
          else{$('[name="pemrakarsa"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.ttd) {$('[name="ttd"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.ttd);}
          else{$('[name="ttd"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.urusan) {$('[name="urusan"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.urusan);}
          else{$('[name="urusan"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.bahasa) {$('[name="bahasa"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.bahasa);}
          else{$('[name="bahasa"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.subjek) {$('[name="subjek"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.subjek);}
          else{$('[name="subjek"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          if (response.error.teu) {$('[name="teu"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.teu);}
          else{$('[name="teu"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
         
          // if (response.error.file) {$('[name="file"]').addClass('is-invalid').nextAll('.invalid-feedback:first').html(response.error.file);}
          // else{$('[name="file"]').removeClass('is-invalid').nextAll('.invalid-feedback:first').html('');}
          console.log('masih eror');
          console.log(response.error);
        }else{
          Swal.fire("Sukses", response.sukses, "success");
          $("#modalFormProduk").modal("hide");
          $("#tb_produk").DataTable().ajax.reload();
          console.log('harusnya bisa');
        }
        // console.log($('[name="nomor"]').val());
      },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log($("[name=produk]").val());
        Swal.fire("Error!", xhr.status + "\n" + xhr.responseText + "\n" + thrownError, "warning");
      },
    }).done(function (resp) {
      // spinner.hide();
    });
  });

Controller:
Code:
function tambahProduk()
{
if ($this->request->isAJAX()) {
$validation = \Config\Services::validation();

$valid = $this->validate([
'jenis_id' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Jenis tidak boleh kosong.']],
'tentang' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Tentang tidak boleh kosong.']],
'nomor' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Nomor tidak boleh kosong.']],
'tahun' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Tahun tidak boleh kosong.']],
'bidang' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Bidang tidak boleh kosong.']],
'tempat_penetapan' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Tempat Penetapan tidak boleh kosong.']],
'tgl_penetapan' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Tanggal Penetapan tidak boleh kosong.']],
'tgl_undang' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Tanggal Pengundangan tidak boleh kosong.']],
'sumber' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Sumber tidak boleh kosong.']],
'pemrakarsa' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Pemrakarsa tidak boleh kosong.']],
'ttd' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Penandatangan tidak boleh kosong.']],
'urusan' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Urusan tidak boleh kosong.']],
'bahasa' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Bahasa tidak boleh kosong.']],
'teu' => ['rules' => 'required', 'errors' => ['required' => 'Kolom T.E.U. tidak boleh kosong.']],
'subjek' => ['rules' => 'required', 'errors' => ['required' => 'Kolom Subjek tidak boleh kosong.']],
]);

if (!$valid) {
$msg = ['error' => [
'jenis_id' => $validation->getError('jenis_id'),
'tentang' => $validation->getError('tentang'),
'nomor' => $validation->getError('nomor'),
'tahun' => $validation->getError('tahun'),
'bidang' => $validation->getError('bidang'),
'tempat_penetapan' => $validation->getError('tempat_penetapan'),
'tgl_penetapan' => $validation->getError('tgl_penetapan'),
'tgl_undang' => $validation->getError('tgl_undang'),
'sumber' => $validation->getError('sumber'),
'pemrakarsa' => $validation->getError('pemrakarsa'),
'ttd' => $validation->getError('ttd'),
'urusan' => $validation->getError('urusan'),
'bahasa' => $validation->getError('bahasa'),
'teu' => $validation->getError('teu'),
'subjek' => $validation->getError('subjek'),
// 'produk' => $validation->getError('produk'),
]];
echo json_encode($msg);
} else {
$file_produk = $this->request->getFile('produk');
$file_produk->move('assets/doc/produk');
$produk = $file_produk->getName();

$dataproduk = [
'jenis_id' => $this->request->getVar('jenis_id'),
'tentang' => $this->request->getVar('tentang'),
'nomor' => $this->request->getVar('nomor'),
'tahun' => $this->request->getVar('tahun'),
'bidang' => $this->request->getVar('bidang'),
'tempat_penetapan' => $this->request->getVar('tempat_penetapan'),
'tgl_penetapan' => $this->request->getVar('tgl_penetapan'),
'tgl_undang' => $this->request->getVar('tgl_undang'),
'sumber' => $this->request->getVar('sumber'),
'pemrakarsa' => $this->request->getVar('pemrakarsa'),
'ttd' => $this->request->getVar('ttd'),
'urusan' => $this->request->getVar('urusan'),
'bahasa' => $this->request->getVar('bahasa'),
'teu' => $this->request->getVar('teu'),
'subjek' => $this->request->getVar('subjek'),
// 'uji_materi' => $this->request->getVar('uji_materi'),
'produk' => $produk,
// 'abstrak' => $this->request->getVar('abstrak'),
// 'status' => $this->request->getVar('status'),
];

$this->p->insert($dataproduk);
$msg = [
'sukses' => 'Data berhasil disimpan.'
];

echo json_encode($msg);
}
} else {
exit('Maaf tidak dapat diproses');
}
}

Form:
Code:
<form method="POST" id="formProduk" enctype="multipart/form-data">
<?= csrf_field() ?>
<input type="text" id="id_produk" name="id_produk" style="display: none">
<div class="row gy-2">
<div class="col-md-4">
<div class="form-group">
<label class="form-label" for="tentang">Tentang</label>
<div class="form-control-wrap">
<textarea class="form-control no-resize" name="tentang" placeholder="Tentang"></textarea>
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-md-8">
<div class="row gy-2">
<div class="col-md-4">
<div class="form-group">
<label class="form-label" for="nomor">Nomor</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="nomor" placeholder="Nomor">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-label" for="tahun">Tahun</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="tahun" placeholder="Tahun">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-label" for="jenis_id">Jenis</label>
<div class="form-control-wrap ">
<div class="form-control-select">
<select class="select-produk-jenis" name="jenis_id" placeholder="Pilih Jenis Produk">
</select>
<div class="invalid-feedback"></div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-label" for="tempat_penetapan">Tempat Penetapan</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="tempat_penetapan" value="Pekalongan" placeholder="Tempat Penetapan">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-label">Tanggal Penetapan</label>
<div class="form-control-wrap">
<div class="form-icon form-icon-left">
<em class="icon ni ni-calendar"></em>
</div>
<input type="text" class="form-control date-picker" name="tgl_penetapan" data-date-format="dd-mm-yyyy" placeholder="Tanggal Penetapan" autocomplete="off">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-label">Tanggal Pengundangan</label>
<div class="form-control-wrap">
<div class="form-icon form-icon-left">
<em class="icon ni ni-calendar"></em>
</div>
<input type="text" class="form-control date-picker" name="tgl_undang" data-date-format="dd-mm-yyyy" placeholder="Tanggal Pengundangan" autocomplete="off">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<div class="row gy-2">
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="bidang">Bidang</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="bidang" placeholder="Bidang">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="sumber">Sumber</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="sumber" placeholder="Sumber">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="pemrakarsa">Pemrakarsa</label>
<div class="form-control-wrap ">
<div class="form-control-select">
<select class="select-instansi" name="pemrakarsa" placeholder="Pilih Instansi">
</select>
<div class="invalid-feedback"></div>
</div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="ttd">Penandatangan</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="ttd" placeholder="Penandatangan">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="urusan">Urusan</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="urusan" placeholder="Urusan">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="bahasa">Bahasa</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="bahasa" value="Indonesia" placeholder="Bahasa">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="subjek">Subjek</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="subjek" placeholder="Subjek">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="teu">T.E.U.</label>
<div class="form-control-wrap">
<input type="text" class="form-control" name="teu" placeholder="Tajuk Entry Utama">
<div class="invalid-feedback"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="uji_materi">Uji Materi</label>
<div class="form-control-wrap">
<div class="form-file">
<input type="file" class="form-file-input" name="uji_materi">
<label class="form-file-label" for="uji_materi">Pilih berkas</label>
<input type="text" class="form-control" name="uji_materis" placeholder="Belum ada berkas." disabled>
</div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="abstrak">Abstrak</label>
<div class="form-control-wrap">
<div class="form-file">
<input type="file" class="form-file-input" name="abstrak">
<label class="form-file-label" for="abstrak">Pilih berkas</label>
<input type="text" class="form-control" name="abstraks" placeholder="Belum ada berkas." disabled>
</div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="file">File Produk</label>
<div class="form-control-wrap">
<div class="form-file">
<input type="file" class="form-file-input" name="produk">
<label class="form-file-label" for="produk">Pilih berkas</label>
<input type="text" class="form-control" name="produks" placeholder="Belum ada berkas." disabled>
<div class="invalid-feedback"></div>
</div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="status_akhir">Status</label>
<div class="form-control-wrap ">
<div class="form-control-select">
<select class="select-biasa" name="status_akhir">
<option value="Berlaku">Berlaku</option>
<option value="Tidak Berlaku">Tidak Berlaku</option>
</select>
</div>
</div>
</div>
</div>
</div>
<br>
<div id="ket_status" class="row gy-2">
<?php for ($i = 0; $i < 8; $i++) { ?>
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="status">Keterangan Status</label>
<div class="form-control-wrap ">
<div class="form-control-select">
<select class="select-biasa" name="status[]" placeholder="Pilih Keterangan Status">
<option value=""></option>
<option value="Mencabut">Mencabut</option>
<option value="Dicabut">Dicabut</option>
<option value="Mengubah">Mengubah</option>
<option value="Diubah">Diubah</option>
</select>
</div>
<div class="form-control-select">
<select class="select-produk" name="status_id[]" placeholder="Pilih Produk">
</select>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<br>
<div class="row gy-2">
<div class="col-sm-3">
<div class="form-group">
<label class="form-label" for="catatan">Catatan</label>
<div class="form-control-wrap">
<textarea class="form-control no-resize" name="catatan" placeholder="Catatan"></textarea>
</div>
</div>
</div>
</div>
<br>
<div class="row gy-2">
<div class="col-sm-12" align="right">
<button type="submit" class="btn btn-primary" id="btnTambahProduk">Simpan</button>
<button type="submit" class="btn btn-primary" id="btnUbahProduk">Simpan</button>
<button type="button" class="btn btn-danger" id="btnBatalProduk" data-bs-dismiss="modal">Batal</button>
</div>
</div>
</form>
Thanks in advance.
Reply
#2

Frist, comfirm routes:
https://codeigniter4.github.io/CodeIgnit...ing-routes
Reply
#3
Photo 

(09-14-2023, 10:02 PM)kenjis Wrote: Frist, comfirm routes:
https://codeigniter4.github.io/CodeIgnit...ing-routes

this is the result
Code:
+--------+-------------------------+--------------------+--------------------------------------------------------------------+----------------+---------------+
| Method | Route                  | Name              | Handler                                                            | Before Filters | After Filters |
+--------+-------------------------+--------------------+--------------------------------------------------------------------+----------------+---------------+
| GET    | /                      | »                  | \App\Controllers\home                                              | session        | toolbar      |
| GET    | home                    | »                  | \App\Controllers\home                                              | session        | toolbar      |
| GET    | produk                  | »                  | \App\Controllers\Produk::index                                    | session        | toolbar      |
| GET    | produk/getProduk        | »                  | \App\Controllers\Produk::getProduk                                | session        | toolbar      |
| GET    | instansi/getInstansi    | »                  | \App\Controllers\Instansi::getInstansi                            | session        | toolbar      |
| GET    | produk/getProdukAll    | »                  | \App\Controllers\Produk::getProdukAll                              | session        | toolbar      |
| GET    | produk/getProdukJenis  | »                  | \App\Controllers\Produk::getProdukJenis                            | session        | toolbar      |
| GET    | register                | »                  | \CodeIgniter\Shield\Controllers\RegisterController::registerView  | session        | toolbar      |
| GET    | login                  | »                  | \CodeIgniter\Shield\Controllers\LoginController::loginView        |                | toolbar      |
| GET    | login/magic-link        | magic-link        | \CodeIgniter\Shield\Controllers\MagicLinkController::loginView    |                | toolbar      |
| GET    | login/verify-magic-link | verify-magic-link  | \CodeIgniter\Shield\Controllers\MagicLinkController::verify        |                | toolbar      |
| GET    | logout                  | »                  | \CodeIgniter\Shield\Controllers\LoginController::logoutAction      | session        | toolbar      |
| GET    | auth/a/show            | auth-action-show  | \CodeIgniter\Shield\Controllers\ActionController::show            |                | toolbar      |
| POST  | produk/tambahProduk    | »                  | \App\Controllers\Produk::tambahProduk                              | session        | toolbar      |
| POST  | register                | »                  | \CodeIgniter\Shield\Controllers\RegisterController::registerAction | session        | toolbar      |
| POST  | login                  | »                  | \CodeIgniter\Shield\Controllers\LoginController::loginAction      |                | toolbar      |
| POST  | login/magic-link        | »                  | \CodeIgniter\Shield\Controllers\MagicLinkController::loginAction  |                | toolbar      |
| POST  | auth/a/handle          | auth-action-handle | \CodeIgniter\Shield\Controllers\ActionController::handle          |                | toolbar      |
| POST  | auth/a/verify          | auth-action-verify | \CodeIgniter\Shield\Controllers\ActionController::verify          |                | toolbar      |
+--------+-------------------------+--------------------+--------------------------------------------------------------------+----------------+---------------+
Reply
#4

You don't have the route `GET produk/tambahProduk`.
Reply
#5

(09-14-2023, 11:43 PM)kenjis Wrote: You don't have the route `GET produk/tambahProduk`.

I use POST for the form, why should I have GET route?
I've tried replacing my POST route to GET, the form can run but does not save to the database
Reply
#6

(09-20-2023, 06:26 PM)legendarypokemon69 Wrote: I use POST for the form, why should I have GET route?

Because you request it. If it is really not needed, your JavaScript seems to send unneeded GET request.

(09-20-2023, 06:26 PM)legendarypokemon69 Wrote: I've tried replacing my POST route to GET, the form can run but does not save to the database

Because you removed the POST route.
Reply
#7

I think you want the JavaScript to look like this:

PHP Code:
    $.ajax({
      urlsiteurl "produk/" method "Produk",
      method"POST"

'method' not 'type'. I'm not sure if POST needs to be all caps or not but that's what I'm using.
Hope that helps.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB