Ajax调用在解析json响应时给出错误;笨

时间:2017-06-09 13:56:03

标签: php json ajax codeigniter

看起来非常简单,但在尝试解析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"}}

1 个答案:

答案 0 :(得分:2)

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

<TabControl.Resources>
    <!-- ... -->
    <DataTemplate x:Key="GeneralTabItemTemplate">
        <TextBlock Text="General" />
    </DataTemplate>
    <DataTemplate x:Key="PersonTabItemTemplate">
        <TextBlock Text="{Binding FirstName}" />
    </DataTemplate>
</TabControl.Resources>

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

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

    Properties prop = new Properties();
    InputStream in = getClass().getClassLoader().getResourceAsStream("dme.properties");
    FileOutputStream out = null;
    try {
         prop.load(in);}  // load all old properties
    catch (IOException e) {}
    finally {try { in.close(); } catch (IOException e) {} }
    prop.setProperty("a", "b"); //new property
    try {
        out = new FileOutputStream("dme.properties");
        prop.store(out, null);} //overwrite
    catch (IOException e) {} 
    finally {try {out.close();} catch (IOException e) {} }
  }

您只需使用您的对象而无需解析它。