簡體   English   中英

如何分別傳遞ajax調用參數

[英]how to pass ajax call parameters separately

我有一個將參數發送到控制器的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);
            }
        });
    }

但是當我嘗試在控制器中使用print_r時,它同時顯示了(day12)和兩個ID,因此將數據傳遞給控制器​​的方式正確嗎?

嘗試這個:

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

在您的php控制器中,您可以使用$ _POST獲得這些值。 (print_r($ _ POST))數組。

使用下面的代碼來調用帶有參數的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

        }

     });
}

您還應該嘗試像下面的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