views:

302

answers:

1

Hi!

I am using an XMLHttpRequest to POST a JSON string to PHP. The JSON object is created in JavaScript and using the JSON2.js from json.org to create an JSON string representing the object.

JSON.stringify(object);

Whenever the object contains a string which has a special character in it, e.g. é, JavaScript does not give any error but PHP receives an empty array

[]

Is there a JavaScript function which produces the exact same resutls as the PHP function

htmlentities()

The data is send via POST, so the following functions

escape()
encodeURI()
encodeURIComponent()

are a bit overkill.

Thanks!

+2  A: 

Even when sending stuff via POST, you still need to correctly urlencode. If the ampersand character is in the JSON body, this would be treated as a parameter/value pair separator and your JSON would no longer be valid.

escape() is deprecated so use encodeURIComponent(). It shouldn't be overkill as this is one of the intended purposes of the function.

Andy E