Ajax函数参数传递到codeigniter中的控制器函数



我正在开发代码点火器。我正在视图页面中调用ajax函数。Ajax函数正在调用控制器方法。Ajax函数包含3个参数,我想将其传递给控制器方法,但由于某种原因,我无法访问来自Ajax函数的参数。

我的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/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应该是这样的:

$.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;
}

相关内容

  • 没有找到相关文章

最新更新