1

I am using the auto complete plugin by Devbridge and I have it all installed here is my code:

$(document).ready(function(){
    $('#request_task').autocomplete({
      serviceUrl: '<%= ajax_path %>',
      minChars:1,
      width: 300,
      delimiter: /(,|;)\s*/,
      deferRequestBy: 0, //miliseconds
      params: { artists: 'Yes' },
    });

});

This request hits my rails action and returns this json. there is only one object returned but most of the time there will be more then 1...this was just a test case:

[
    {
        "user": {
            "salt": "somthing",
            "name": "john",
            "encrypted_password": "92dadsfa6b001ffe71c3c1d8e9fb76c42d1c8afeffa739de9063d94206c",
            "created_at": "2010-09-10T14:10:54Z",
            "updated_at": "2010-09-10T14:10:54Z",
            "admin": null,
            "id": 1,
            "remember_token": "c945522b3eb0a25e36bb39155fc05b3eec301ac5e2196956f2e6f86b4b22c987",
            "email": "[email protected]"
        }
    }
]

I can clearly see the request in firebug but I am not getting anything for the autocomplete and it errors out...Am i missing anything...My error is

a.suggestions is undefined

3 Answers 3

4

I think you need to read a little further down the developers page as your response is in the wrong format:

Web page that provides data for Ajax Autocomplete, in our case autocomplete.ashx will receive GET request with querystring ?query=Li, and it must return JSON data in the following format:

{
 query:'Li',
 suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
 data:['LR','LY','LI','LT']
}

Notes:

query - original query value
suggestions - comma separated array of suggested values data
(optional) - data array, that contains values for callback function when data is selected.

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

2 Comments

This may be the problem as I am not sure about this plugin. But if it the case, it may not be the right solution as you are having tight coupling with format. Try docs.jquery.com/Plugins/Autocomplete/autocomplete which can support any format and has very good plugging in capability using very good callback method support
I wouldn't see this as tightly coupled in the strictest sense. I would expect that my ViewModel (or server side code) would query my data source and format the output accordingly for the View which, in this case, is the Autocomplete. From what I've seen of your alternative you are still going to have to return either an array of strings or an array of key=>value pairs. The documentation isn't good enough to know if your output format above will be read. Lastly, if you don't need all the data in that format, why send it to the client? You could reveal something you shouldn't.
1

Sincere advice , dont construct JSON Strings. Please go for an API.
If you are using java, check this out http://www.json.org/java/
and make sure to set content-type in response as application/json

Comments

0

YOUR JSON is in a wrong format

Check their correct format

{
 query:'Li',
 suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
 data:['LR','LY','LI','LT']
}

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.