【问题标题】:How to post the content from ajax to controller in codeigniter?如何将内容从ajax发布到codeigniter中的控制器?
【发布时间】:2015-09-15 09:43:03
【问题描述】:

我无法将我的数据发布到控制器。我正在使用 codeigniter 框架。我需要对数据库执行自动完成操作。我无法通过 ajax 将输入或选择的数据从视图发送到控制器。这是我的 ajax 函数:

var i=$('table tr').length;

$(".addmore").on('click',function(){
    count=$('table tr').length;

    var data="<tr><td><input type='checkbox' class='case'/></td><td><span id='snum"+i+"'>"+count+".</span></td>";
    data +="<td><input class='form-control' type='text' id='countryname_"+i+"' name='countryname[]'/></td> <td><input class='form-control' type='text' id='country_no_"+i+"' name='country_no[]'/></td><td><input class='form-control' type='text' id='phone_code_"+i+"' name='phone_code[]'/></td><td><input class='form-control' type='text' id='country_code_"+i+"' name='country_code[]'/></td></tr>";
    $('table').append(data);
    row = i ;
    $('#countryname_'+i).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : "<?php echo base_url(); ?>admin/billing/get_country",
            dataType: "json",
            method: 'post',
            data: {
               name_startsWith: request.term,
               type: 'country_table',
               row_num : row
            },
             success: function( data ) {
                 response( $.map( data, function( item ) {
                    var code = item.split("|");
                    return {
                        label: code[0],
                        value: code[0],
                        data : item
                    }
                }));
            }
        });
    },
    autoFocus: true,            
    minLength: 0,
    select: function( event, ui ) {
        var names = ui.item.data.split("|");
        id_arr = $(this).attr('id');
        id = id_arr.split("_");                 
        $('#country_no_'+id[1]).val(names[1]);
        $('#phone_code_'+id[1]).val(names[2]);
        $('#country_code_'+id[1]).val(names[3]);
    }               
  });

【问题讨论】:

  • admin/billing/get_country 是否设置为接收和处理$_POST 数据?
  • 你在做正确的事,只要确保 admin/billing/get_country 是一个有效的 url,其中 admin 是您的控制器名称,而 billing 是该控制器内部的函数
  • 我已将路线设置为 admin/billing/get_country 调用 admin_billing/get_country
  • 我需要将数据发布到 admin/billing/get_country @user2959229
  • Web 控制台是否显示该网址的代码状态 200?如果没有,请检查您的 base_url 函数。如果是,请确保您的控制器正在调用该 url 的正确函数。

标签: php jquery ajax codeigniter


【解决方案1】:

改变这个

url : "<?php echo base_url(); ?>admin/billing/get_country",

对此,

 url     :   '<?php echo site_url('admin/billing/get_country');?>',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2014-02-11
    相关资源
    最近更新 更多