Problem with JSON generated via AJAX

Chris Bruce's Avatar

Chris Bruce

04 Jun, 2010 08:18 PM

Hello,

I've got the AutoSuggest plugin set up and I am using ajax to get results via a MySQL query to return to autosuggest. It seems that somehow autosuggest is choking on the JSON that it being generated by the external page. If I write the JSON that is generated to a file and then use is directly in the page, it works:

var data = [{value: "3298", name: "Pitcher's Pond Golf"},{value: "20528", name: "Pitman Golf Course"},{value: "2283", name: "Pitt Meadows Golf and Country Club"},{value: "23413", name: "Pittsboro Golf Course"},{value: "28441", name: "Pittsburg's Delta View Golf Course"},{value: "20971", name: "Pittsburgh Field Club"},{value: "37688", name: "Pittsburgh National Golf Club"},{value: "33823", name: "Pittsburgh North Golf Club"}];
$("div.webName input").autoSuggest(data, {selectedItemProp: "name", searchObjProps: "name"});

however, if I call the page via ajax, it doesn't:

$("div.webName input").autoSuggest("/Golf-Marketing-Services/suggest.php", {selectedItemProp: "name", searchObjProps: "name"});

At first, it would return "No results found", but I have fixed that issue and now it just doesn't return anything.

Here is the link: http://www.golfmax.com/Golf-Marketing-Services/where2play.php

I am using UTF-8 for all pages and data, and I can't seem to find out what else I am missing. Does the JSON need to be formatted any different than this on the page called by AJAX?

[{value: "3298", name: "Pitcher's Pond Golf"},{value: "20528", name: "Pitman Golf Course"},{value: "2283", name: "Pitt Meadows Golf and Country Club"},{value: "23413", name: "Pittsboro Golf Course"},{value: "28441", name: "Pittsburg's Delta View Golf Course"},{value: "20971", name: "Pittsburgh Field Club"},{value: "37688", name: "Pittsburgh National Golf Club"},{value: "33823", name: "Pittsburgh North Golf Club"}]

Thanks in advance for any help.

  1. 1 Posted by Mike Murray on 08 Jun, 2010 04:05 PM

    Mike Murray's Avatar

    It looks like you were able to get this working after looking at the website you posted. How did you get around this issue? I seem to be having the same problem.

    Thanks,

    Mike

  2. 2 Posted by Chris Bruce on 08 Jun, 2010 06:29 PM

    Chris Bruce's Avatar

    First off, I changed the call:

    $("div.webName input").autoSuggest("/Golf-Marketing-Services/suggest.php", {selectedItemProp: "name", searchObjProps: "name", retrieveComplete:function(data){ return eval(data); }});

    Also, since I am running a version of PHP that doesn't support the json_encode function, I had to download a php class that would provide this support:

    http://www.extjs.com/forum/showthread.php?2362-Getting-the-JSON-exa...

    After doing all that it worked!

  3. 3 Posted by Mike Murray on 08 Jun, 2010 06:33 PM

    Mike Murray's Avatar

    Great thanks for getting back to me so quickly. I just figured it out as well. Once I added "retrieveComplete" things seemed to work.

    Although now I'm having trouble removing the values from the hidden field. The first one I add always seems to hang around, even after removing the selection. Did you or are you having any issues similar to this?

  4. 4 Posted by Chris Bruce on 08 Jun, 2010 06:43 PM

    Chris Bruce's Avatar

    Hmmm, I haven't noticed that issue. I'm posting the form through and using PHP to handle that. I had to tinker about a bit to get the preFill to work on form post.

  5. 5 Posted by Chris Bruce on 08 Jun, 2010 06:47 PM

    Chris Bruce's Avatar

    Yeah, this is a little annoying. If you add something from the list, remove it, add something else and then post the form, the value from the original add gets posted. We've got to find a solution for this...

  6. 6 Posted by Saurabh Pareek on 29 Jun, 2010 01:25 PM

    Saurabh Pareek's Avatar

    Any update on this issue?

    Also, my JSON is working fine but there is one issue. If I select a value from list, still it remains in the list and I can re-select the same value. This should not happen. Once selected, it should disappear from the list. Any workaround for this?

  7. 7 Posted by jason on 25 Aug, 2010 04:05 AM

    jason's Avatar

    @Saurabh, I am having the same problem. Have you found a solution?

  8. 8 Posted by Paolo on 15 Jul, 2011 05:47 PM

    Paolo's Avatar

    @Chris Bruce: Thanks a lot for that tip on using eval: retrieveComplete:function(data){ return eval(data); }

    That really helped me. Thanks a lot.

  9. 9 Posted by Michael Murray on 15 Jul, 2011 05:56 PM

    Michael Murray's Avatar

    On Fri, Jul 15, 2011 at 1:47 PM, Paolo <
    [email blocked]> wrote:

  10. 10 Posted by Paolo on 15 Jul, 2011 06:06 PM

    Paolo's Avatar

    I am now getting the same bug as listed by Michael Murray with the first value hanging around. I'm guessing no resolution to this has been found considering it was posted in 201. Fail! Seems like this project has kind of been forgotten about. Which is a shame as it's probably the best autosuggest plugin available.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac