7

Can not json_decode a GET parameter

 3 years ago
source link: https://www.codesd.com/item/can-not-json-decode-a-get-parameter.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Can not json_decode a GET parameter

advertisements

I'm trying to pass a stringified json object as a GET parameter but the receiving URL seems unable to decode it, I am not sure why.

Here is the relevant code:

Client side Json object production (works fine):

function createJson(){
// This works fine.  It creates a json objects with three elements, the third being an array.
//(omitted code for brevity)
  return jsonData;
}

Client side ajax call (works fine):

function recordSetGet(jsonData){
  request = createRequest();
  var rawSet=JSON.stringify(jsonData);
  var encodedSet=encodeURIComponent(rawSet);
  var params="set="+encodedSet;
  var url= "Ajax_recordSetGet.php?"+params;
  request.open("GET", url, true);
  request.onreadystatechange = function(){}
  request.send(null);
}

This yield the following URL: Ajax_recordSetGet.php?set=%7B%22setTitle%22%3A%22test%22%2C%22setTags%22%3A%22test%20%22%2C%22set%22%3A%5B%7B%22first%22%3A%22Joe%22%2C%22last%22%3A%22Doe%22%2C%22checked%22%3Atrue%7D%5D%7D"

Server side treatment:

<?php
header('Content-Type:text/html; charset=UTF-8');
if(!session_id()){
  session_start();
}
if(isset($_GET['set'])){
  $set=$_GET['set'];//This is the URI encoded string
  var_dump ($set);
  var_dump (json_decode($set));
  var_dump ("Json last error is ".json_last_error());
}
?>

The result of the var_dumps are: string '{"setTitle":"test","setTags":"test ","set":[{"first":"Joe","last":"Doe","checked":true}]}"' (length=90)
null
string 'Json last error is 4' (length=20)

So why is json_decode() failing here? The json_last_error() result suggests a syntax error.

Edit: Note that json_decode also fails if I send a non encoded string:
If I build the param string without encoding it like this:

var rawSet=JSON.stringify(jsonData);
var params="set="+rawSet;
var url= "Ajax_recordSetGet.php?"+params;

Then the URL becomes Ajax_recordSetGet.php?set={"setTitle":"test","setTags":"test ","set":[{"first":"Joe","last":"Doe","checked":true}]}"

And the receiving URL var_dump yield the same error:
string '{"setTitle":"test","setTags":"test ","set":[{"first":"Joe","last":"Doe","checked":true}]}"' (length=90)
null
string 'Json last error is 4' (length=20)


I don't know why, but you have a trailing double quote in your URL that messes up the JSON decode process.

...%7D%5D%7D"   <- Trailing double quote

This quote also shows up in your dump of the set variable:

...true}]}"' (length=90) <- double quote is inside the string, the single quote is part of the var_dump output.

This double quote must be introduced in the Javascript code, although I cannot see a spot where that should happen. But your debugging should take place in that part of the system.

Note that you MUST NOT urldecode the string passed as the "set" parameter, as PHP has done the decoding for you already. But with the trailing double quote you cannot call json_decode().

As a quick proof of concept, try using $set = rtrim($set, '"') in your PHP code before decoding to remove that quote character.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK