gurpal2000
gurpal2000

Reputation: 1082

jquery-autocomplete json object how to parse

im using jquery-autocomplete (http://code.google.com/p/jquery-autocomplete)

what i don't understand is how to parse a complex json object to it. So far with "remoteDataType: 'json'" all i can specify is a "name" and "value".

But i want to pass in a large json object "data" with other fields such as

name: Mr Smith, address: 180 my road, value: 12345

so it seems to understand a name and value as a key-val. But how do i get address in the showResult or onItemSelect functions?

Thanks

Upvotes: 1

Views: 595

Answers (1)

Tomasz Zieliński
Tomasz Zieliński

Reputation: 16346

I have no problems with serving JSON responses like this:

[
    {
        value: 'Mr Smith, 180 My Rd, 12345',
        data: {
            name: 'Mr Smith',
            address: '180 My Rd',
            value: '12345'
        }
    },
    ...
]

The catch is that if you use jQuery.ajax, then the interpretation of returned JSON changes depending on Content-Type header - if it's 'application/json' then you have to turn off "remoteDataType: 'json'", beacuse jQuery pre-parses JSON string and returns a Javacript object.

Upvotes: 1

Related Questions