【问题标题】:Multiple parameter passing in ajax call in MVCMVC中ajax调用中的多个参数传递
【发布时间】:2018-07-20 10:54:55
【问题描述】:

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

【问题讨论】:

  • 也显示你的控制器方法
  • 请始终通过提供一些 cmets 来回应答案,或者,如果对您有帮助,请将其标记为绿色并点赞,这是感谢所有程序员的最佳方式

标签: php ajax codeigniter codeigniter-3


【解决方案1】:

希望对您有所帮助:

由于您将 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

【讨论】:

  • 如果我的回答对你有帮助,请检查它是否为绿色并支持我
猜你喜欢
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 2010-12-27
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多