【问题标题】:CodeIgniter Ajax callCodeIgniter Ajax 调用
【发布时间】:2018-09-01 17:12:20
【问题描述】:

ajax 调用没有命中控制器功能,是什么原因,我不明白的原因请指导我

我正在尝试向控制器发送 ajax 调用更新记录这是我的 ajax 代码

$(document).ready(function() {

$(".update").click(function(event) {
	debugger;
event.preventDefault();
var vehno = $("input#vn").val();
var vbrand = $("input#vb").val();
var vmodel = $("input#vm").val();
var vcolor = $("input#vcol").val();
debugger;
$.ajax({
type: "ajax",
method:"Post",
url:'<?php echo base_url('index.php/vehicleCtrl/FunUpdate')?>',
//async:false,
dataType: 'json',
data: {vehicle: vehno, brand: vbrand, vmodel:vmodel,vcolor:vcolor},
success: function(res) {
	alert("working");
// if (res)
// {
// // Show Entered Value
// jQuery("div#result").show();
// jQuery("div#value").html(res.username);
// jQuery("div#value_pwd").html(res.pwd);
// }
},
error:function(res){
alert(res);
}
});
});
});

这个控制器的功能

这是一个codeigniter控制器

 public function FunUpdate()
 {
 $as= $this->input->post('vehicle');
 $id=-1;
 $vehicleArray = array('vehicleNo' => $this->input->post('vehicle'),
'Brand' => $this->input->post('brand'),
'Model' => $this->input->post('vmodel'),
'Color' => $this->input->post('vcolor'),
    );
    echo json_encode($vehicleArray);
        $Result=$this->VehicleModel->Update($vehicleArray,$no);
        if($Result)
        {
          $data= array('error' =>'Vehicle Update Successful');
          $data["DetailList"]=$this->VehicleModel->FunDetailSearch($no);
          $data["EditTrack"]=$this->VehicleModel->EditTrackDetail($id);
          $data["NewVehicle"]=$this->VehicleModel->FunfindVehicle($no);
          $this->load->view('Layout/header');
      $this->load->view('vehicle/create',$data);
      $this->load->view('Layout/footer');
  }
}

【问题讨论】:

  • 这可能无法解决问题,但type: "ajax" 是错误的。 'type' 是 'method' 的别名(你已经得到了)。只需删除类型。
  • 也许你写错了 url。您应该已经编写了控制器名称和函数名称。 url : = base_url()?>vehicleCtrl/FunUpdate

标签: javascript php jquery ajax codeigniter


【解决方案1】:
ajax request should look like this

                $('#buttonid').click(function(){
                    var vehno = document.getElementById('vehno').value;
                    var brand = document.getElementById('brand').value;
                    $.ajax({
                        url:'<?=base_url()?>index.php/Controller/function',
                        method: 'post',
                        data: {vehno: vehno, brand: brand},
                        dataType: 'json',
                        success: function(response){
                            alert('data updated');    
                        }
                    });
                }); 

function

        public function updateDetails(){
        // POST data
        $postData = $this->input->post();

        //load model
        $this->load->model('Main_model');

        // get data
        $data = $this->Main_model->updateVehicle($postData);

        echo json_encode($data);
    }

Modal

    function updateVehicle($postData){

        $response = array();

        if($postData['id'] ){

        $this->db->where('id',$postData['id']);
        return $this->db->update('vehicle',$postData);          

        }

希望您现在使用 Ajax 更新您的数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2015-01-17
    • 2017-06-14
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多