I want to send json string encoded data to the PHP backend. For doing so, I am using an GET
paremeter with URL encoded json data in it in form of an array like this one: ["mystring1","mystring2"]
When I now try to decode it using php's json_decode
function, it returns null
. Also, using this input string, doesn't change anything at all: {"mykey":["mystring1","mystring2"]}
.
Here's my full code example:
JavaScript:
var myArr = [];
myArr.push('mystring1');
myArr.push('mystring2');
window.location.href = 'example.com/index.php?myparam=' + encodeURIComponent(JSON.stringify(myArr));
PHP backend site:
$jsonStr = filter_input(\INPUT_GET, 'myparam', \FILTER_SANITIZE_STRING);
var_dump($jsonStr);
var_dump(json_decode($jsonStr, true));
The first var_dump
prints correctly the json string, the second one returns null
. Using the assoc flag (2nd json_encode
parameter) doesn't change anything.
What I expected to get:
array(2) { [0]=> string(9) "mystring1" [1]=> string(9) "mystring2" }
What I actually got:
NULL
var_dump
for $jsonStr
returns the following for me:
string(41) "["mystring1","mystring2"]"