php - parseJson is running into an unexpected error (Unexpected identifier) -
this in same script. know difference between ajax , serverside code. have php interwoven in javascript script follows:
<script> <?php $json_string = json_encode(array("agent_table" => "<span style='float: left;'></span>")); ?> var response = $.parsejson('<?php echo $json_string; ?>'); </script> but parsejson complaining of error. error disappears when remove styling span.
the error 'unexpected identifier'. in chrome.
you have ' characters in data. these go on represented literal ' characters in json.
you embedding json straight javascript string, , string delimited ' characters. first ' appears as data in json terminate js string.
this general problem when embedding on info format in another. embedding json in javascript in html.
your options:
replace every instance of' in json \' (so mean "apostrophe" instead of "end of js string") treat json javascript object literal instead of trying munge string run through json parser. the latter alternative sane 1 (so long goal doesn't involve proving php can generate valid json).
var response = <?php echo $json_string; ?>; you don't need worry farther escaping inserting html because sequence need worry (inside script element) </script> , php's json_encode output <\/script> anyway.
php jquery
No comments:
Post a Comment