douhai5835 2018-04-12 07:24
浏览 65

CI中的Ajax无法正常工作

This is the first time I am working with CI+AJAX+JSON.

I have a controller Admin.php inside controllers directory of Codeigniter. The controller code is as under:

public function getmylist()
{
    $users_arr[] = array("sno" => "1", "myname" => "hello");
    echo json_encode($users_arr);
}

Then, in views, I have a view with the following code:

<select class="form-control" name="mod_countries" id="mod_countries">
  <?php
    if(isset($countrylist))
      foreach($countrylist as $c)
        echo "<option value=" . $c->cname . ">".$c->cname;
  ?>
</select>

Then my target select which i need to populate from ajax is

<select class="form-control" name="mod_newlist" id="mod_newlist">
</select>

My ajax is as under:

$(document).ready(function() {
        $('#mod_countries').change(function(event) {
            var cname = $("select#mod_countries").val();
            alert(cname);
            $.ajax({
                    type: "post",
                    url: "<?php echo base_url(); ?>" + "Admin/getmylist",
                    dataType: "json",
                    data: {mcname: cname},
                    success: function(res){
                        if(res) {

                            var len=res.length;
                            $("#mod_newlist").empty();
                            for(var i=0; i<len; i++)
                            {
                                var sno=res[i]['sno'];
                                var myname=res[i]['myname'];
                                $("#mod_newlist").append("<option value='" + sno + "'>"+myname+"</option>");
                            }
                        }
                        else
                        {
                            console.log('hitting');
                        }
                    },
                    error: function(res, status, error) {
                        var err = res.responseText;
                        alert(res.Message);
                        alert(status);
                        alert(error);
                    }

            });
        });
    });

When I select an item from the first dropdown list, I get the selected item. Now I expect the second drop downlist to get an item from the controller's code. but, my code goes into error block and alert shows following error messages for each alert line of code:

undefined

parsererror

SyntaxError: Unexpected token I in JSON at position 0

Please tell me where I am making a mistake?

Also, please help with regards to how should I return the sno->name pair from model because at lat, i want to populate the new select from database.

  • 写回答

2条回答 默认 最新

  • dtvhqlc57127 2018-04-12 07:58
    关注

    Remove debug/garbage data from response, like I am called. If still not work then parse JSON in success as following

    var res = $.parseJOSN(res);
    if(res) {
    .....
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测