Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem with parsing json in jquery autocomplete
    primarykey
    data
    text
    <p>I'm having trouble in parsing my JSON data in jQuery autocomplete. My JSON comes from this code:</p> <pre><code>&lt;cfset theQ = lcase(q)&gt; &lt;cfquery datasource="#source#" name="qry" maxrows="20"&gt; select top 10 lastname from info where lower(lastname) like '#theQ#%' order by lastname &lt;/cfquery&gt; &lt;!--- Before we can serialize the query, we need to convert it to an array of structs. ---&gt; &lt;cfset rows = [] /&gt; &lt;!--- Loop over the query to convert it. ---&gt; &lt;cfloop query="qry"&gt; &lt;!--- Create a row struct. ---&gt; &lt;cfset row = {} /&gt; &lt;!--- Add each column to our struct. ---&gt; &lt;cfloop index="column" list="#qry.columnList#" delimiters=","&gt; &lt;cfset row[ column ] = qry[ column ][ qry.currentRow ] /&gt; &lt;/cfloop&gt; &lt;!--- Append the row struct to the row array. ---&gt; &lt;cfset arrayAppend( rows, row ) /&gt; &lt;/cfloop&gt; &lt;!--- Now that we have converted our query to an array of structs, we can serialize it using the serializeJSON() method. ---&gt; &lt;cfset serializedQuery = serializeJSON( rows ) /&gt; &lt;cfoutput&gt;#serializedQuery#&lt;/cfoutput&gt; </code></pre> <p>I converted my query into array of structures and then serialized it to JSON. Now, parsing this JSON in jQuery autocomplete does not work. The data I get looks like this:</p> <pre><code>[{"lastname":"abc"},{"lastname":"def"},{"lastname":"ghi"}] </code></pre> <p>Here is the code for autocomplete:</p> <pre><code>$(document).ready(function() { $("#name").autocomplete("data/name.cfm",{ minChars:1, delay:10, autoFill:false, matchSubset:false, matchContains:1, cacheLength:10, selectOnly:1, dataType: 'json', parse: function(data) { var parsed = []; var dataParsed = $.parseJSON(data); for (var i = 0; i &lt; dataParsed.length; i++) { parsed[parsed.length] = { data: dataParsed[i], value: dataParsed[i].lastname, result: dataParsed[i].lastname }; } return parsed; }, formatItem: function(item) { return item; } }); </code></pre> <p>When I type in the text field, I get the whole JSON string as the search result. I've looked into other codes for parsing but still I can't get it working. Any help? Thanks.</p> <p>References: Simon Whatley for autocomplete; Ben Nadel for query to array of structs and serialize to json</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload