1

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"}}

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh that I was expecting doing silly mistake. Thanks Callombert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.