简体   繁体   中英

Ajax call giving error on parsing json response; codeigniter

Seems very simple but giving unknown error in ajax success result when trying to parse json response. Error Msg: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data See below code: View:

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);
                    }
                });
            }

Controller:

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);      
}

Model:

    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();

        }
    }

Response of ajax:

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

Remove this in your controller. You're outputting a PHP object and a JSON object, Ajax will only read a JSON object.

print_r($result);

If you specify dataType: json, you have to give it json.

Also unless I'm missing something you don't need this:

 parsedobj = JSON.parse(result);

You can just use your object without parsing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM