http://qs321.pair.com?node_id=988876

sushmas.sharma has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl page where I am using the jQuery autocompleter. I am calling another perl script which generates json output. The page works but not all the time. Here are my scripts. index.pl
<script type="text/javascript" src="/xyz/jquery/js/jquery-1.7.1.min +.js"></script> <script type="text/javascript" src="/xyz/jquery/js/jquery-ui-1.8.17 +.custom.min.js"></script> <script type="text/javascript"> \$(document).ready(function(){ var byname = { source: "searchbyname.pl", select: function(event, ui){ \$("#cn").val(ui.item.cn); \$("#xyzid").val(ui.item.xyzid); \$("#xyzuid").val(ui.item.xyzuid); \$("#mail").val(ui.item.mail); }, minLength:2 }; \$("#cn").autocomplete(byname); } </script> <input type="text" name="cn" id="cn" value="$cn" size="40" maxlength=" +128" onClick="clearForm();" />
searchbyname.pl
my @query_output; # LOOP THROUGH RESULTS while ( my $row = $sth->fetchrow_hashref ){ push @query_output, $row; } my $json = JSON::to_json(\@query_output); &log("I am in search by name page looking for $inpstr returning $json + "); print $json;
When I check the web console in firefox, the output is as follows:
[17:08:56.206] GET http://server.xyz.com/xyz/cgi-bin/searchbyname.pl?t +erm=batt [HTTP/1.1 200 OK 375ms] [17:08:56.579] syntax error @ http://server.xyz.com/xyz/cgi-bin/search +byname.pl?term=batt:1 [17:08:57.830] GET http://server.xyz.com/xyz/cgi-bin/searchbyname.pl?t +erm=batte [undefined 375ms]
I start the search with string batt, The first line where is shows the status as 200 OK, the auto completer shows up with the results, even if it is complaining in the next line about the syntax of the json. then I press next letter e and the autocompleter does not come up. the firefox console shows the request/response in the third line and it says undefined. I am printing the json passed to index.pl in the log and the output is there and I validated the output online and it comes back as valid json. I have no idea, what am I doing wrong. please help. Thanks, Sushma

Replies are listed 'Best First'.
Re: perl jquery script returns http status undefined sometimes
by derby (Abbot) on Aug 21, 2012 at 22:13 UTC

    Well ... for starters, the problem could be loads of different things but I'm guessing the results from your DB query are not in the format jquery autocomplete expects them to be:

    The data from local data, a url or a callback can come in two variants +: An Array of Strings: [ "Choice1", "Choice2" ] An Array of Objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]
    Also ... I'm unsure but you may have to set the content-type to 'application/json' on the response. I do that but I cannot remember if I *needed* to or if I just thought it was good form.

    As for the console, the autocomplete widget has some delays built into it so it doesn't swamp the server, if you type normal speed, you may not see an ajax request for each letter -- try typing slower to see ajax call per letter.

    -derby
Re: perl jquery script returns http status undefined sometimes
by Anonymous Monk on Aug 21, 2012 at 21:26 UTC
    No way