0

I am trying to implement an search function (auto complete) using jquery,jquery-ui in Codeigniter, but I am getting "JSON Parse unexpected Character error" when typing into the input field.

When calling /user/search_userautocomplete/test I do get back some values in JSON.

(With warning : The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.)

Does it have something to do with Content-Type? I am really stuck for the last hours. I would really appreciate if someone knows what I am doing wrong. Thanks

Controller:

// Search User autocomplete
function search_userautocomplete($title)
{
    if(!empty($title) || isset($title))
    {
        $this->db->like('firstname',$title);
        //$this->db->or_like('lastname',$title);

        echo json_encode( $this->db->get('users_profiles')->result() );

    }
}

user_autocomplete.js

$(document).ready(
    function() {

        $('#search').keypress(
                function(e) {

                    if (e.which == 13) {
                        e.preventDefault();
                    }

                    var searched = $('#search').val()

                    var fullurl = $('#hiddenurl').val()
                            + 'user/search_userautocomplete/'
                            + searched;

                    $.getJSON(fullurl, function(result) {
                        var elements = [];
                        $.each(result, function(i, val) {
                            elements.push(val.title)
                        })
                        $('#search').autocomplete({
                            source : elements
                        })

                    })

                })
    })

searchfield_userautocomplete (View)

<div class="row-fluid  tagline">
        <div class="span8">

            <form>

                <label for="search">Search</label> <input id="search" type="text">
                <input value="" id="hiddenurl" type="hidden"> <input
                    value="Submit" type="submit">
            </form>


            <script src="<?php echo base_url();?>js/jquery.js"
                type="text/javascript"></script>
            <script src="<?php echo base_url();?>js/jquery-ui.js"
                type="text/javascript"></script>
            <script src="<?php echo base_url();?>js/user_autocomplete.js"
                type="text/javascript"></script>

        </div>

    </div>
zer02
  • 3,963
  • 4
  • 31
  • 66
  • Can you show the json output that you get back? – Karan Punamiya Aug 09 '12 at 14:52
  • 1
    Why aren't you using remote datasource autocomplete instead of constructing the source every time manually? – user1190992 Aug 09 '12 at 15:25
  • [{"id":"5","firstname":"Vladmir","lastname":"Jobs","address":"austria road 1","zipcode":"40000","country":"Austria","telephone":null,"mobilephone":null,"website":null,"bio":"BODYBUILDiNG","occupation":null,"profilepicture":"\/files\/profilepicture\/stevejobs.jpg"},{"id":"6","firstname":"Admin","lastname":"ISTRATOR","address":"here","zipcode":"5000","country":"GER","telephone":null,"mobilephone":null,"website":null,"bio":"Siteadmin","occupation":null,"profilepicture":"\/files\/profilepicture\/Admin.png"}] – zer02 Aug 10 '12 at 07:59
  • So I managed to use jQuery autocomplete via remote source. And I reduced my json string. Seems to work for now. – zer02 Aug 10 '12 at 08:18

1 Answers1

0

The reason for the "JSON Parse unexpected Character error" might be that the url for getJSON is not constructed properly. I think it must be:

var fullurl = $('#hiddenurl').val()
              + '/user/search_userautocomplete/'
              + searched;

As for the character encoding error, refer this:

Character encoding not declared in html document

Community
  • 1
  • 1
Karan Punamiya
  • 8,603
  • 1
  • 26
  • 26
  • I am using : `var fullurl = $('#hiddenurl').val() + 'index.php/user/search_userautocomplete/' + searched;` – zer02 Aug 10 '12 at 08:00