I’ve been trying to send a JSON string to PHP server like this:
$.ajax({
type: "POST",
url: "themes.php?page=themeoptions",
data: {structure : JSON.stringify(structure)},
});
However, every quotation mark from the string I send is escaped automatically, so I can’t decode it in PHP with json_decode(). I could remove all the escape characters, but I really doubt that this way of sending JSON data to server is safe.
So, I was wondering if you have any ideas on how to do that in a simple and safe (not necessarily bulletfroof) way?
Thank you!
You can just do
'{"structure":' + JSON.stringify(structure) + '}'or{structure: structure}The first one is a JSON String so jQuery doesn’t need to parse it. The second is a javascript object, so jQuery knows exactly how to parse it.
But you are mixing the two, so jQuery is confused and re-encodes your object, because you encoded only half your object.
So another alternative would be
JSON.stringify({structure: structure })