【问题标题】:Ajax call giving error on parsing json response; codeigniterAjax 调用在解析 json 响应时出错;代码点火器
【发布时间】:2017-11-11 14:00:22
【问题描述】:

看起来很简单,但在尝试解析 json 响应时,ajax 成功结果中会出现未知错误。错误消息:SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符 见下面的代码: 查看:

function getUsertype(){
            var region= $('#regionSel option:selected').text();

                $.ajax({
                    type:"GET",
                    url: "<?php echo base_url('Pricecomparison/getUsertype'); ?>",
                    data:{ "region":region},
                    contentType: "application/json",
                    datatype: "json",
                    success: function(result){
                         //parsedobj = JSON.parse(result);
                         console.log(result);
                         parsedobj = JSON.parse(result);

                         var appenddata="<option value = '0'>Select User Type</option>";
                         var appendmetertype;
                         if (parsedobj.reg) 
                         {
                            $.each(parsedobj.reg, function(index, value) 
                            {
                                appenddata += "<option value = '" + value.usertype + "'>" + value.usertype + " </option>";    
                            });
                            $('#usertypeSel').html(appenddata); 
                         }
                         else
                         {
                            $('#usertypeSel').html(appenddata); 

                            for (i=0; i<=4; ++i){
                                appendmetertype="<option value = '0'>Select Meter Type "+i+"</option>";
                                $("#MeterTypeVar"+i+"Sel").html(appendmetertype );
                            }
                         }
                    },
                    error: function(xhr, textStatus, error){
                        console.log(xhr.statusText);
                        console.log(textStatus);
                        console.log(error);
                    }
                });
            }

控制器:

function getUsertype()
{
    $this->load->model('Settings_model');
    $region = $this->input->get('region');

    $result =   array("reg" => $this->Settings_model->getSelectedUsertype($region));
    print_r($result);
    echo json_encode($result);      
}

型号:

    public function getSelectedUsertype($region)
    {
        #Create main query
        $this->db->select('usertype');
        $this->db->where('region', $region);
        $this->db->group_by('region','usertype');
        $q = $this->db->get('res_price');

        if ($q->num_rows()> 0 )
        {
            return $q->row();

        }
    }

ajax 的响应:

Array
    (
      [reg] => stdClass Object
       (
        [usertype] => LOW - GS20
       )
    )
      {"reg":{"usertype":"LOW - GS20"}}

【问题讨论】:

    标签: php json ajax codeigniter


    【解决方案1】:

    在您的控制器中删除它。您正在输出一个 PHP 对象和一个 JSON 对象,Ajax 只会读取一个 JSON 对象。

    print_r($result);
    

    如果你指定dataType:json,你必须给它json。

    除非我遗漏了一些你不需要的东西:

     parsedobj = JSON.parse(result);
    

    你可以直接使用你的对象而不用解析它。

    【讨论】:

    • 哦,我以为我会犯愚蠢的错误。谢谢卡隆伯特。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2011-06-04
    • 2019-05-15
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多