Posts Tagged ‘php’

Encode your JSON data before sending it

January 18th, 2012 No comments

A problem I was facing recently, that the JSON was being receive properly on Firefox 3.6 my work development machine. Finished the project and deployed it. I haven’t tested it on other machines, how clever am I?! I opened my laptop to see how it would appear on other browsers, and shockingly, it wasn’t working at all!

After spending hours trying to locate the line where the error occurs and find out what’s the problem it turned out that I have to decode the JSON output before sending it. I wasn’t using any PHP built-in functionality for JSON, that’s because at work, we are still using PHP4 which has none. I was basically writing the JSON output explicitly. The text output can contain some special characters that need to be encoded and escaped before sending it. After digging around a bit I found out a solution from this post. Everything is pretty much explained there.

In a nutshell, you’ll have to use json_encode() to encode the data from an array and then output it. A JSON library is provided along with that post. There is also a decoding function, but I haven’t use it since I’m receiving my data on a javascript.

Get Adobe Flash player