I experience the problem with parsing json encoded object in js.
JSON.parse(word_array); with error Uncaught SyntaxError: Unexpected identifier
My investigation showed that object word_array does not exist because of wrong-formation in PHP: it has a unescaped single quote' inside, thus making js consider it as the end of the string.
I form encoding next way:
echo "<script>var word_array = '";
echo json_encode($word_set);
echo "';\n";
As far as I know, json_encode should escape all undesirable charactets like ' but it does not.
What might be the problem?
My php version: Version PHP: 5.3.13
And $word_set is array of assoc. array:
$word_set = array();
while($stmt->fetch())
{
$word_set_tmp[] = array(
'word' => $word,
'definition' => $def
);
array_push ($word_set,$word_set_tmp);
}
var word_array = '[{"word":"Abeyance","definition":"a temporary suspension of activity."},{"word":"Akimbo","definition":"having one's hands in a bent position on the hips."}one's'shouldnt be escaped