I send data to function.php
method: 'POST',
JSON.stringify( { username1: 'example1', username2: 'example2' } ),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
How do i analyze this data and return each key and value separately?
username1 or example1 or username2 or example2
I tried this
$data = json_encode($_POST);
echo $data;
But this code returns a strange object, where all data is in the key and the value is empty {{"username1":"example1","username2":"example2"}: ""}
This also doesn't work
$data["username1"]; // return only {
$data->username1; // return empty
I don't know if this will help
var_dump($_POST);
array(1) {
["{"username1":"example1","username2":"example2"}"]=>
string(0) ""
}
and
var_dump(json_encode($_POST));
string(62) "{"{\"username1\":\"example1\",\"username2\":\"example2\"}":""}"
If what your asking for is calling on values within an object being returned from json I think this may help (providing i understand your answer)
var obj = JSON.parse(data); // This will parse the json data thats being returned
var username = obj[0].username1;
console.log(username);
That should log in the console the value of the JSON object you created.
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.