php/js JSON not coming through valid

I’m running php ver 5.4.31. I’m having problems parsing the JSON I’m sending to my php files.
My goal is to have an object to use on the other side for db work. I’ve got logs on the functions before and after to watch all the traffic and here is what I’m getting.

I’ve been scouring the web and tried a lot of different solutions, I’m adding the closest I’ve been able to come which generates a string vs an object.

Read More

any help is appreciated, I’ve been banging my head against this for hours now.

JS/Json sent over:

     $.get('my.php', {
                sc_user: JSON.stringify(obj)
            });

console.log: Sent over: {“id”:42876678,”kind”:”user”

php attempt One:

  $plan_B = $_GET['sc_user'];
  echo $plan_B;

“”{“id”:42876678,”kind”:”user”

php string generation:

   $sc_user = json_decode(stripslashes(json_encode($_GET['sc_user'])));

output: {“id”:42876678,”kind”:”user”

json_decode:

$sc_user = json_decode($_GET['sc_user']);

output: “”

Related posts

Leave a Reply

1 comment

  1. Based upon what you are showing, the JSON is invalid. There is no closing bracket.

    {"id":42876678,"kind":"user"
    

    should be

    {"id":42876678,"kind":"user"}
    

    Try validating what you are sending via json lint, if the raw json string is invalid it is not likely that any of the json processing tools will be able to handle it:

    http://jsonlint.com

    Also, as a side note, if you are sending json, it is a best practice to ensure that the content type is set as such:

    Returning JSON from a PHP Script