简体   繁体   English

如何分别传递ajax调用参数

[英]how to pass ajax call parameters separately

I have a java script ajax method which send parameters to controller 我有一个将参数发送到控制器的Java脚本ajax方法

function class_selection(day) {

        var section_id = document.getElementById('section_id2').value;
        var class_id = document.getElementById('class_id').value;
       // alert("day"+day);
        alert("class_id"+class_id);
        alert("section_id"+section_id);

        $.ajax({
            url: '<?php echo base_url(); ?>index.php?admin/get_class_section/' + day + section_id +  class_id,
           // alert("url"+url);
            success: function(response)
            {
                jQuery('#section_selection_holder').html(response);
            }
        });
    }

but when I try with a print_r in controller it shows (day12) both the day with both ids so the way pass data to the controller is correct? 但是当我尝试在控制器中使用print_r时,它同时显示了(day12)和两个ID,因此将数据传递给控制器​​的方式正确吗?

Try this: 尝试这个:

 $.ajax({
        url: '<?php echo base_url(); ?>index.php?admin/get_class_section/',
       data: {day: day, section_id: section_id, class_id: class_id},
       method: POST,
        success: function(response)
        {
            jQuery('#section_selection_holder').html(response);
        }
    });

and in your php controller you can get these values using $_POST. 在您的php控制器中,您可以使用$ _POST获得这些值。 (print_r($_POST)) array. (print_r($ _ POST))数组。

use below code to call ajax with parameter 使用下面的代码来调用带有参数的ajax

 function class_selection(day) {

    var section_id = document.getElementById('section_id2').value;
    var class_id = document.getElementById('class_id').value;
    $.ajax({
        type: "POST",
        dataType: 'json',
        data: 'day=' + day + '&sectionId=' section_id + '&classId=' + class_id,
        url: 'index.php?admin/get_class_section',
        success: function(json) {
          //your response here

        }

     });
}

you should also try to send data like below ajax call 您还应该尝试像下面的ajax调用一样发送数据

var frm_data = {day: day,section_id:section_id,class_id:class_id}
$.ajax({
   url: '<?php echo base_url(); ?>index.php?admin/get_class_section/',
   type: 'POST',
   data: frm_data,
   dataType:'JSON',
   success: function(response)
   {
        jQuery('#section_selection_holder').html(response);
   }
});

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

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