繁体   English   中英

在MVC中的Ajax调用中传递多个参数

[英]Multiple parameter passing in ajax call in MVC

我正在研究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参数值发送到控制器方法?

希望这个能对您有所帮助 :

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

您的控制器方法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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM