SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data (Php Codigniter And Jquery)

1.8k Views Asked by At

I am trying to implement jQuery Autocomplete in my Codeigniter project, but I get the Following error in Firefox.

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

JavaScript

jQuery("#search").autocomplete({
    source: function(req, response) {
        $.ajax({
            url: "<?php echo site_url('C_systemUser/search_value'); ?>",
            dataType: "text",
            success: function(data) {
                var re = $.ui.autocomplete.escapeRegex(req.term);
                var matcher = new RegExp("^" + re, "i");
                response($.grep(data, function(item) {
                    return matcher.test(item.label);
                }));
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
        event.preventDefault();
        $('#search').val(ui.item.label);
        $('#id').val(ui.item.value);
    }
});

Modal

function systemUserSearch($q) {
    $this - > db - > select('systemUserID,systemUserName');
    $this - > db - > like('systemUserName', $q);
    $query = $this - > db - > get('systemuser');
    if ($query - > num_rows() > 0) {
        foreach($query - > result_array() as $row) {
            $result_row['label'] = htmlentities(stripslashes($row['systemUserName']));
            $result_row['value'] = htmlentities(stripslashes($row['systemUserID']));
            $result[] = $result_row;
        }
        echo json_encode($result);
    }
}

Control

public function search_value()
{
    $q=$this->input->get('query');
    $this->load->model('m_autocomplete');
    $this->m_autocomplete->systemUserSearch($q);
}
4

There are 4 best solutions below

1
Jean-Luc Barat On

Are you sure about this :

    url: "<?php echo site_url('C_systemUser/search_value'); ?>",

Are you well render js in php file ?

2
Ihab On

you should set the Content-Type header

header("Content-Type: application/json;charset=utf-8");

in addition, set in your JS dataType: "json"

3
AudioBubble On

Javascript

jQuery("#search").autocomplete({
    source: function(req, response) {
        $.ajax({
            url: "<?php echo site_url('C_systemUser/search_value'); ?>",
            dataType: "json",
            success: function(data) {
                var re = $.ui.autocomplete.escapeRegex(req.term);
                var matcher = new RegExp("^" + re, "i");
                response($.grep(data, function(item) {
                    return matcher.test(item.label);
                }));
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
        event.preventDefault();
        $('#search').val(ui.item.label);
        $('#id').val(ui.item.value);
    }
});

Modal

function systemUserSearch($q) {
    $this - > db - > select('systemUserID,systemUserName');
    $this - > db - > like('systemUserName', $q);
    $query = $this - > db - > get('systemuser');
    if ($query - > num_rows() > 0) {
        foreach($query - > result_array() as $row) {
            $result_row['label'] = htmlentities(stripslashes($row['systemUserName']));
            $result_row['value'] = htmlentities(stripslashes($row['systemUserID']));
            $result[] = $result_row;
        }
        ob_clean();
        header('Content-Type: application/json');
        echo json_encode($result);
        ob_end_flush();         
    }
}
0
Ameer Manathat On

Thanks All for your help Fixed the error, its because of jquery Ui Reference error changed to new version and the error fixed