ASP Json with JQuery UI Autocomplete

Hi Experts,

This issue has been frustrating me for about three hours so could do with a few more opinions on it!

I am using jquery UI autocomplete and would like to use an ASP file which returns JSON content as the source.

The ASP file appears to work fine:
http://mar.sfwebs.co.uk/json/companies.asp

When I copy the data that code generates directly into the javascript, it works fine...however, when I use that pages address as the source it does not. As can be seen in this example:

http://mar.sfwebs.co.uk/demo.asp

I have set up http://mar.sfwebs.co.uk/json/companies.asp to write each line of the output to a text file so that I can see that the javascript is access it, and confirm it all looks as it should.

So...what gives?!

Any help much appreciated!

Thanks,

Dan

---

Code which generates the companies.asp listing:

response.ContentType() = "application/json"

Dim MMb_connMb_STRING
MMb_connMb_STRING = "xxxxxxx"

recCompanies__vterm = Request.QueryString("term")

Dim recCompanies
Dim recCompanies_cmd
Dim recCompanies_numRows

Set recCompanies_cmd = Server.CreateObject ("ADODB.Command")
recCompanies_cmd.ActiveConnection = MMb_connMb_STRING
recCompanies_cmd.CommandText = "SELECT TOP 20 cId, cName FROM dbo.Companies WHERE cName LIKE ? OR cFullName LIKE ?" 
recCompanies_cmd.Prepared = true
recCompanies_cmd.Parameters.Append recCompanies_cmd.CreateParameter("param1", 200, 1, 255, "%" + recCompanies__vterm + "%") ' adVarChar
recCompanies_cmd.Parameters.Append recCompanies_cmd.CreateParameter("param2", 200, 1, 255, "%" + recCompanies__vterm + "%") ' adVarChar


Set recCompanies = recCompanies_cmd.Execute
recCompanies_numRows = 0

start = 0

response.write("[")

DO while NOT recCompanies.EOF 
IF start <> 0 THEN
response.write(",")
END IF 

response.write("{ label: """&(recCompanies.Fields.Item("cName").Value)&""", value: """&(recCompanies.Fields.Item("cId").Value)&"""}")
 
start = 1
recCompanies.MoveNext()
LOOP 
response.write("]")

recCompanies.Close()
Set recCompanies = Nothing

Open in new window

stressfreewebsAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
Your error is you DON'T return a VALID json object.

JSON key need to be surrounded with double quotes, example :
[{"label":"Marbank","value":"1"}]
and NOT :
[{label:"Marbank",value:"1"}]

fix this and your code will work!
0
 
stressfreewebsAuthor Commented:
PS. begin typing je to get some results in the first box.
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
$( "#tags2" ).autocomplete({

means place the data in the ID tags2.  Your "doesn't work" is just id="tags"  Change the code in your js to  $( "#tags3" ).autocomplete({  then change

<p><strong>Doesn't Work:</strong></p>
<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags" />
</div>  

to

<p><strong>Doesn't Work:</strong></p>
<div class="ui-widget">
  <label for="tags3">Tags: </label>
  <input id="tags3" />
</div>  

and see what happens
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Probably does not matter here, but if you are doing an xmlhttp post, the page you are posting to needs to be in a different folder then the originating page.
0
 
stressfreewebsAuthor Commented:
Hi Padas,

Thank you for taking a look. #tags is still a valid ID so it should work - I have changed it to #tags3 anyway just to see, but it hasn't sorted it.

The file is in a different folder so that should be ok.

Thanks,

Dan
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I think I read your question wrong.  The problem you are having is loading from the external file.  Ok you need to load the data somehow.  http://jqueryui.com/autocomplete/#remote-jsonp

For asp and json I also use http://code.google.com/p/aspjson/ for more complex json data
0
 
stressfreewebsAuthor Commented:
I was using the second option described in the source field here:

http://api.jqueryui.com/autocomplete/#option-source

Is that not suitable for what I need?
0
 
leakim971PluritechnicianCommented:
Line 32 :
response.write("{ "label": """&(recCompanies.Fields.Item("cName").Value)&""", "value": """&(recCompanies.Fields.Item("cId").Value)&"""}")
0
 
stressfreewebsAuthor Commented:
THANK YOU!

Frustratingly simple, I wish I'd asked sooner!

Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.