[support] How to generate a correct JSON response?

Tim Beelen tim at abit.nl
Wed Jul 11 15:25:07 UTC 2007


Dear list,
I'm having some problems getting autocomplete to work. I think the 
problem is in the durpal_to_js() function. This is what I figured out so 
far using greasemonkey (Firefox plugin):
How it looks when the autocomplete DOES work:
http://blog.monstuff.com/XmlHttpRequestTracing: [678] intercepted load: 
200 { "": "", "Oscar Wilde": "Oscar Wilde", "Tim": "Tim" }
This is the reply when it doesn't:
http://blog.monstuff.com/XmlHttpRequestTracing: [141] intercepted load: 200

{ "": "", "Oscar Wilde": "Oscar Wilde", "Tim": "Tim" }

I know for sure the problem is those two returns at the beginning of the 
reply (I tested it).

This is the code I use to generate the reply (example code on drupal.org):
function myautocomp($string) {
  $matches = array();
  $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) 
LIKE LOWER('%%%s%%')", $string, 0, 10);
  while ($user = db_fetch_object($result)) {
    $matches[$user->name] = check_plain($user->name);
  }
  print drupal_to_js($matches);
  exit();
}

Please help, it is driving me nutty!

With best regards,
Tim



More information about the support mailing list