I have an javascript array which is json encoded and posted to PHP. when I check a value in the array before posting ie console.log(selection[878][2824])
I'm getting a value as its result. and then the variable is json encoded using JSON.stringify
and posted to server. But when I json_decode
the value in php and printed the same, I'm getting null array
js script
console.log(selection);
$http({
method: 'POST',
url: 'example.php',
data: 'selection='+JSON.stringify(selection),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
php script
$selectionData = json_decode($_POST['selection']);
print_r($selectionData[878][2824]);
So what is happening while encoding the data here. How is it lost ?
似乎数据格式不正确,我认为您必须格式化字符串
"selection :" +JSON.stringify(selection);
try this
js
$http({
method: 'POST',
url: 'example.php',
data: {selection : JSON.stringify(selection)}
})
php
$data = json_decode(file_get_contents("php://input"),true);
$selection = json_decode($data['selection'],true);
You do not have to stringify your content when you are using POST, just set the right headers.
$http({
method: 'POST',
url: 'example.php',
data: selection,
headers: {'Content-Type': 'application/json'}
})
or if you need it with an identifier:
$http({
method: 'POST',
url: 'example.php',
data: {"selection": selection },
headers: {'Content-Type': 'application/json'}
})
and in PHP (if you are using the additional identifier):
$selectionData = $_POST['selection'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.