0

Sorry if there is a decent example of this somewhere outer, I've been looking for days and can't find a solution.

Gathering data from JSON file "database.json":

"[{\"id\":\"1\",\"state\":\"Alaska\"},{\"id\":\"2\",\"state\":\"Alabama\"},{\"id\":\"3\",\"state\":\"California\"}]"

Here is my most recent attempt that failed:

$("#state").autocomplete({
                       source: function( request, response ) {
                       $.ajax({
                           url: "database.json",
                           dataType: "json",
                           data: {term: request.term},
                           success: function(data) {
                                     console.log(data)
                                       response($.map(data, function(item) {
                                       return {
                                           label: item.state,
                                           id: item.id,
                                       };
                                   }));
                               }
                           });
                       },
                       minLength: 2,
                       select: function(event, ui) {
                           $('#state_id').val(ui.item.id);
                       }
                   });

And of course the html:

<input type="text" id="state"/>

I have no idea where to go from here, I just want a simple drop down autocomplete. Let me know if more information is needed and I can provide.

Thank you!

14
  • any error in your console? Commented Sep 10, 2014 at 2:02
  • 2
    if the ajax request returns the value, it looks fine - jsfiddle.net/arunpjohny/dqggjdj8/1 Commented Sep 10, 2014 at 2:04
  • @ArunPJohny No error in console. I just simply don't see the values dropped down. Does that mean there is an issue with the accessibility of "database.json"? Commented Sep 10, 2014 at 2:10
  • 1
    in the success handler log the value of data like console.log(data) Commented Sep 10, 2014 at 2:12
  • 1
    data is the param to the success callback... so you need to add the console logging inside the success callback just before response($.map(data, function(item) { Commented Sep 10, 2014 at 2:22

1 Answer 1

1

With a lot of help from @ArunPJohny I was able to get the out complete to work with the addition of

data  = JSON.parse(data); 

just below success: function(data) {

The problem was that my JSON was saved as a string and not strict JSON so I needed to parse it first.

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

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.