I've a very nasty problem sending data from PHP to Javascript.
Long story short... I get some data from a DB then convert all to json and send all to a javascript function.
One of the column from the DB contain data that look like:
val01\test\val04
I assign these data to an array then using json_encode convert everything to json, like in this example:
$result = 'val01\test\val04'; //these are the data that are get from DB
$example = ['testData' => $result];
json_encode($example);
At the end I've these data:
{"testData":"val01\\test\\val04\\"}
now, when i use these result with parse.json like this:
var json = '{"testData":"val01\\test\\val04\\"}';
obj = JSON.parse(json);
I receive an error:
Error: Unexpected token v in JSON at position 24
seem that the slashes are escaped incorrectly... To be sure that is not my fault with something else.. I test my example also on MDN sample page (https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) that return same result.
How can I solve this problem?
I won't create my own escape function.. is too dangerous from my point of view.. I never know what kind of data I will can find in this columns so.. there is a clean way to handle this problem?
thank you