Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax call in codeigniter not as I expected

When I call ajax in codeigniter 3.1.7 than it can take automatically base url in ajax url

AJAX CALLING CODE :

   $(document).on("click",".btn_edit",function(){
        var id = $(this).attr("id");
        $("#btnsave").attr("mode","update");
        $("#myModal").modal("show");
        console.log(id);
        $.ajax({
            type :"post",
            url :"Home/get_emp",
            data :{"id":id},
            dataType : "json",
            success : function(edit_feed){
                $("#txtempid").val(edit_feed.e_id);
                $("#txtempname").val(edit_feed.e_name);
                $("#selempskill option:contains('"+edit_feed.e_skill+"')").prop('selected',true);
                $("#prev_img").attr("src",base_url+edit_feed.e_img);
                var lbl = edit_feed.e_img.split("/");
                $("#pic_label").html(lbl[3]);       
            }
        });
    });

Controller :

class Home extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->model('Emp_model');
        }
        public function index(){
            //$this->load->view('index');
            $this->load->view('home');
        }
        public function get_emp(){
            $id=$_POST['id'];
            $data = $this->Emp_model->get_emp($id);
            echo json_encode($data);
        }

ERROR :

jquery.3.2.1.js:3049 POST http://localhost/cod_std/Home/get_emp 404 (Not Found)

Here I want call only Controllers Home and its function get_emp please give me some solution I use codeigniter3.1.7

like image 328
Sani mori Avatar asked Jan 25 '26 23:01

Sani mori


1 Answers

You can try:

url:"<?php echo site_url("Home" . '/get_emp'); ?>",
like image 183
Ranjith Avatar answered Jan 27 '26 13:01

Ranjith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!