I have a JSON that is getJSON
ed to a PHP script in order to update the database for a simple CMS:
{
"pid": "3",
"post": "<p><span style='text-decoration: underline;'><strong>Test</strong></span></p><br><p>blah</p><br><p> </p><br><p><span style='text-decoration: underline;'><strong>Test</strong></span></p><br><p>blah</p>",
"tagline": "",
"title": "About"
}
In PHP it is decoded with json_decode
and then sent off to the database.
It works perfectly and JSONLint reports that the JSON is valid, however PHP's json_decode
fails with error 4, which is 'syntax error'. I'm unsure which is correct (I copied the JSON above from the GET
request I send, so should be valid on JS' side).
I use JSON.stringify
to create the JSON from my array on JS' side. The array is as follows:
var arr = {
pid : "<?php echo $pId; ?>",
post : $("#edit_target_preview").html(),
tagline : document.getElementById('page_tagline').value,
title : document.getElementById('page_title').value,
};
This array is forwarded to the PHP script via getJSON
:
$.getJSON("savepost.php?json=" + JSON.stringify(arr), function(data){
*stuff happens here*
});
What am I doing wrong here? Am I overthinking this, or using completely the wrong approach?
Hexdump:
7b22706964223a2233222c22706f7374223a223c703e3c7370616e207374796c653d5c22746578742d6465636f726174696f6e3a20756e6465726c696e653b5c223e3c7374726f6e673e546573743c2f7374726f6e673e3c2f7370616e3e3c2f703e3c62723e3c703e626c61683c2f703e3c62723e3c703e
"pid": "3",
...` which is not valid JSON. – ibrahim mahrir Jan 29 '17 at 21:00
`, but you copied them from the browser? try using `var_dump` on the variable you get when it doesn't work and copy that, or better, read it from an error file (and not a browser), or use the source of the page. Anyway, try and make a minimal http://sscce.org/ – Nanne Jan 29 '17 at 21:09