6082 comments
2357 subscribers
6220 on Twitter
Subscribe! Feed reader E-mail

Drupal and JQuery 1.5: Fixing the JSON encoding of ampersands

Drupal 6′s drupal_json method encodes ampersands incorrectly for JQuery 1.5, causing the rather cryptic error:

Uncaught Syntax error, unrecognized expression: ...

(If you’re lucky.)

The way to fix this is to borrow the JSON-handling code from Drupal 7. Here’s something you might be able to use:

function yourmodule_json_encode($var) {
  return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), $var);
}

// Fix Drupal JSON problems from http://witti.ws/blog/2011/03/14/jquery-15-json-parse-error
function yourmodule_json($var) {
  drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  if (isset($var)) {
    echo yourmodule_json_encode(json_encode($var));
  }
}

Use yourmodule_json instead of drupal_json wherever applicable.

Hat tip to Greg Payne (Witti) for pointing me in the right direction!

2011-08-04 Thu 14:01

Short URL: http://sachachua.com/blog/p/22402
  • catch

    Or you could also use/review the core patch at http://drupal.org/node/1086098 that should fix this and several other issues.

  • mikeytown2

    Also take a look at the Advanced CSS/JS Aggregation module; works around the issue as well. Function to call is advagg_drupal_to_js().

  • http://www.innoraft.com Mukesh

    Awesome, this saved my life. The code you have put here is a little incomplete, I guess the problem was with the editor. The json_encode function should be:

    function yourmodule_json_encode($var) {
    return str_replace(array(”, ‘&’), array(”, ‘&’), $var);
    }

    And at the end, wherever you are calling drupal_json, just replace with yourmodule_json ..

    Thanks a ton!

  • http://sachachua.com Sacha Chua

    Oh yeah, hey! Good catch. =) Thanks!

On This Day...

  • 2012: Weekly review: Week ending August 3, 2012 — Lots of tech changes! Getting the hang of using my tablet. Upgraded to a new cellphone with a data plan. [...]
  • 2010: Success and blogging — What’s success when you’re writing a personal blog–not a niche blog which you want to make money from through [...]
  • 2008: Finding something worth talking about — "I don’t know what I’d talk about," people often tell me when I encourage them to think of topics for [...]
  • 2008: Emacs and Gnus: zomg, new chapter out the door! — So I _finally_ pulled everything together and got my Gnus chapter out the door. Hooray, hooray, hooray! Reading Mail with Gnus [...]
  • 2008: OpenOffice.org: Replacing dingbats — I _really_ should have blogged this when I first wrote it. That would’ve saved me time writing it again! Sub ReplaceDingbats(optional [...]
  • 2008: Emacs: Caps lock as M-x — Inspired by Lennart Borgman’s recent post on help.gnu.emacs about binding Caps Lock to M-x on Microsoft Windows, I set my [...]
  • 2007: No plans yet — My birthday’s coming up next week and I still don’t have any plans. Somehow, that realization makes me feel lonely. Is this [...]
  • 2006: Month in review — Check out my May, June, July, and August indices, too. =)
  • 2006: CookOrDie: Danger, Will Robinson! — Uh oh. I now know how to bake brownies from scratch, and I can make them as moist and chocolatey as I [...]
  • 2006: Social Tech Brewing: It’s all about choices — (I think about this a fair bit even as a 22-year-old because I want to know my values, and reflecting on [...]
  • 2006: Social Tech Brewing: Women in Technology — Today’s Social Tech Brewing event about Women in Technology gave me much to think about. I’ll blog a bit more about [...]
  • 2005: Chicken adobo results — I microwaved the chicken adobo for another minute and a half just to feel slightly better about it, and fried it [...]
  • 2005: On intelligence and wisdom — On mensaphilopen, Alistair Israel said: The smart ones realize that this it isn’t worth living in this country, and seek their fortune [...]
  • 2005: Finally! Internet! — I had been grumbling about it for all of five minutes as I fiddled about with various settings, finding it completely [...]
  • 2003: FOAF — Friend-of-a-friend descriptions are up at http://sacha.free.net.ph/notebook/foaf.rdf
  • 2003: Invitations — - Berris, Dean Michael – sent - Carreon, Mario – sent - lastname, Aimee – sent c/o Mario - Cimafranca, Dominique – sent - [...]
  • 2003: Women and technical courses — http://www-personal.si.umich.edu/~rfrost/courses/Women+Tech/readings/Margolis.html Link from JM Ibanez
  • 2003: Java — Calen is referring to the “Rethinking the Java Curriculum” article: http://www.onjava.com/pub/a/onjava/2002/08/21/javaxp.html With some interesting follow-up commentary here: http://fishbowl.pastiche.org/archives/000454.html
  • 2003: Enye (n with tilde) — - http://marc.free.net.ph:80/message/19991216.121601.49226c7e.html - http://marc.free.net.ph:80/message/20011213.093356.e7625d7b.html - http://marc.free.net.ph:80/message/20011213.180210.24306ae0.html - http://marc.free.net.ph:80/message/20011214.010850.5f9e91ed.html

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging