(04-14-2020, 06:33 AM)Alexcodeig Wrote: Hey guys, how are you?
I´m trying to use ajax on my ci4 application, but is not working. I want the select boxes changing according the countries and states selected. Please, some help?
.js
________
$( ".pais" ).change(function() {
var id_pais= $("#pais").val();
var base_url= "<?php echo base_url();?>";
$.post(base_url+'/index.php/estado/getEstado', {
id_pais : id_pais
},function (data) {
$("#estado").html(data);
$("#estado").removeAttr('disabled');
});
});
________
Model
________
namespace App\Models;
use CodeIgniter\Model;
class EstadoModel extends Model
{
//Atributos de configuração
protected $table='estado';
protected $primaryKey= 'id';
//Metodo GET
public function getEstado($id_pais){
return $this->asArray()->where(['pais' => $id_pais])->first();
}
}
________
Controller
________
use App\Models\EstadoModel;
class Estado extends BaseController
{
public function getEstado()
{
$id_pais= $this->request->getVar('id_pais');
$model = new EstadoModel();
echo $model->getEstado($id_pais);
}
}
________
Note: Error code 403 to ajax response (I see this in the inspect element on my browser - Network).
add csrf token in ajax data.