On the PHP side of things, I have an integer and I have a string of HTML:
function get_the_goods() { $versionid = '7'; $htmlstuff = ''; $htmlstuff .= '<div id="kittens">STUFF AND STUFF</div><img src="https://example.com">'; $htmlstuff .= '<p>KITTENS THE SEQUEL.</p>'; echo "{'versionid':'" . $versionid . "','htmlstuff':'" . $htmlstuff . "'}"; wp_die(); }
$versionid is just an integer and $htmlstuff is a reasonable string of HTML. I escaped the double quotes trying to resolve this.
When I parse this as such on the JQuery/JS side, I get an unexpected token error. Previously, I was only sending the string of HTML by itself without the JSON array and all was fine.
//THIS IS THE RELEVANT JQUERY/AJAX PART: success : function(data){ if(data && data !="") { var json = JSON.parse(data); var htmlstuff = json.htmlstuff; var previousversion = json.versionid; console.log(versionid); jQuery('#kittens).html(htmlstuff); } }
Advertisement
Answer
My solution to this problem was simple. Instead of attempting to echo the JSON myself, I put it into a PHP array and used JSON_ENCODE to prep it for jQuery and this ended up working fine.