• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ajax on codeigniter 4

#1
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).
Reply

#2
(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.
Reply

#3
Also you should use getPost instead of getVar.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.