簡體   English   中英

Ajax調用在解析json響應時給出錯誤; 笨

[英]Ajax call giving error on parsing json response; codeigniter

似乎非常簡單,但是在嘗試解析json響應時,ajax成功結果中給出了未知錯誤。 錯誤消息:語法錯誤: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將只讀取一個JSON對象。

print_r($result);

如果指定dataType:json,則必須給它json。

另外,除非我缺少任何東西,否則您不需要:

 parsedobj = JSON.parse(result);

您可以只使用對象而無需解析它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM