0

I use below code for jQuery UI Autocomplete.

<script>
$(function() {

    $( "#city" ).autocomplete({
        source: function( request, response ) {

        $.post('<?php echo base_url()?>records/get_village_street_town_name', {
           SourceLanguage: 'SourceLanguage',
           inputVal: $( "#city" ).val()
        },

       function (data) { 
            citydata = jQuery.parseJSON( data );

            response( 

            $.each(citydata, function( index, city ) {
                return city.cityname;
            })

        );


       });



        },
        minLength: 2,
    });
});
</script>

Below is the return O/P via the AJAX call.

[{"cityname":"ABCDE"},{"cityname":"ABDCE"},{"cityname":"ABEDC"}]

The problem is city textbox is not filled with citynames from the AJAX ? How can I fix this ?

I referred below two but didn't help.

JQuery UI autocomplete with json and ajax

Using jquery ui autocomplete + ajax json data

7
  • 1
    Try $.map instead of $.each. Commented Nov 4, 2013 at 5:56
  • 1
    there is no need to $.map() also as there is no data manipulation... is data an array of object Commented Nov 4, 2013 at 5:57
  • if you can please tell me where the issue is Commented Nov 4, 2013 at 5:59
  • You may need to return label: & value: Commented Nov 4, 2013 at 6:02
  • 1
    @ArunPJohny, excellent point. I never even looked at that function body. :P Commented Nov 4, 2013 at 6:05

2 Answers 2

0

Some of your JSON code is invalid and the parser does not identify it as object, in the example you declared [{"cityname":"ABCDE"},{"cityname":"ABDCE"},,{"cityname":"ABEDC"}] which should be [{"cityname":"ABCDE"},{"cityname":"ABDCE"},{"cityname":"ABEDC"}].

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

Comments

0

Working code would be

<script>
$(function() {

    $( "#city" ).autocomplete({
        source: function( request, response ) {

        $.post('<?php echo base_url()?>records/get_village_street_town_name', {
           SourceLanguage: 'SourceLanguage',
           inputVal: $( "#city" ).val()
        },

       function (data) { 
            citydata = jQuery.parseJSON( data );

            response( 

            $.map( citydata, function( item ) {
                return item.cityname;
            })

        );
       });

        },
        minLength: 2,
    });
});
</script>

Comments

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.