【问题标题】:Ajax call in codeigniter not as I expectedcodeigniter 中的 Ajax 调用不像我预期的那样
【发布时间】:2018-08-04 05:22:07
【问题描述】:

当我在 codeigniter 3.1.7 中调用 ajax 时,它可以在 ajax url 中自动获取基本 url

AJAX 调用代码:

   $(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]);       
            }
        });
    });

控制器:

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);
        }

错误:

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

这里我只想调用Controllers Home及其函数get_emp,请给我一些我使用codeigniter3.1.7的解决方案

【问题讨论】:

  • 尝试将网址设为'<?php echo site_url(); ?>'+"Home/get_emp"
  • 甚至/Home/get_emp<?php echo site_url('/Home/get_emp'); ?>

标签: php jquery ajax codeigniter


【解决方案1】:

你可以试试:

url:"<?php echo site_url("Home" . '/get_emp'); ?>",

【讨论】:

    【解决方案2】:

    你必须使用:

    url:"<?php echo base_url(); ?>index.php/Home/get_emp",
    

    【讨论】:

      【解决方案3】:

      使用下面的代码可以解决你的问题,它对我有用

      $(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:"<?php echo base_url(); ?>index.php/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]);       
                      }
                  });
              });
      

      【讨论】:

        猜你喜欢
        • 2012-06-13
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 2021-12-10
        • 1970-01-01
        • 2016-03-07
        • 2020-07-21
        • 1970-01-01
        相关资源
        最近更新 更多