在MVC

时间:2018-07-20 10:54:55

标签: php ajax codeigniter codeigniter-3

我正在研究Codeigniter。我的代码是:

这是我对下拉菜单更改事件的ajax方法调用(查看页面)

$('#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/index.php/mycontroller/contollerfunction',
         success: function(data)
            {
               alert("success");
            }
      });
    });

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

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

获取错误消息:

Message: Missing argument 1 for assetcontroller::controllerfunction(), 
Message: Missing argument 2 for assetcontroller::controllerfunction(), 
Message: Missing argument 3 for assetcontroller::controllerfunction()

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

1 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

由于您将ajax数据发布为post,因此您必须在$this->input->post()方法中使用contollerfunction访问数据

您的控制器方法contollerfunction应该是这样的:

public function contollerfunction()
{
    $costcenterid = $this->input->post('costcenterid');
    $locationid = $this->input->post('locationid');
    $departmentid = $this->input->post('departmentid');

    echo "costcenter= ".$costcenterid;   
    echo "location= ".$locationid;  
    echo "department= ".$departmentid; 
    exit; 
}

注意 :查看浏览器的“网络”标签以查看输出

更多信息:https://www.codeigniter.com/user_guide/libraries/input.html