Ajax函数参数传递给Codeigniter中的控制器函数

时间:2018-07-21 05:32:37

标签: php ajax codeigniter codeigniter-3

我正在研究Codeigniter。我在视图页面中调用ajax函数。 Ajax函数正在调用控制器方法。 Ajax函数包含3个我想传递给控制器​​方法的参数,但是由于某些原因,我无法访问来自ajax函数的参数。

我的ajax方法调用下拉更改事件(查看页面)

Button uploadList = (Button) findViewById(R.id.UpdateList);
uploadList.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        adapter.add(uri.getPath());
        adapter.notifyDataSetChanged();

        Toast.makeText(dropBox.this, "File Added", Toast.LENGTH_SHORT).show();
    }
});

这是我的控制器方法(控制器中的方法)

    $('#drp').change(function(e){  //dropdown change event
          var costcenter = $('#costcenter_id :selected').val(); //parameter 1
          var location1 = $('#location_id :selected').val(); //parameter 2
          var department = $('#department_id :selected').val(); //parameter 3
          $.ajax({
             cashe: false,
             type: 'POST',
             data:  {'costcenterid':costcenter,'locationid':location1,
                     'departmentid':department},
             url: 'http://local.desk.in/mycontroller/contollerfunction',
    success: function(data)
                {
                   alert("success");
                }
          });
        });

获取错误消息:

public function controllerfunction($costcenterid,$locationid,$departmentid)
    {
    echo "costcenter= ". $costcenterid; 
    echo "location= ". $locationid; 
    echo "department= ". $departmentid;
    }

为什么无法将ajax参数值发送到控制器方法?谢谢     前进

1 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

您的ajax应该是这样的:

$.ajax({
    url: "<?=site_url('mycontroller/contollerfunction');?>",
    cache: false,
    type: 'POST',
    data:  {'costcenterid':costcenter, 'locationid': location1, 'departmentid':department},
    success: function(data)
    {
      alert("success");
    }
});

您的控制器方法controllerfunction应该像这样:

使用CI内置$this->input->post()访问此类帖子:

public function controllerfunction()
{
    $costcenterid = $this->input->post('costcenterid');
    $locationid = $this->input->post('locationid');
    $departmentid = $this->input->post('departmentid');
    $posts = array('costcenterid' => $costcenterid,
                   'locationid' =>  $locationid,
                   'departmentid' => $departmentid
             );
    print_r($posts);die;
}