in React I have used redux and passing data like below:
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(user)
};
return fetch('http://localhost:8080/react-crud/api/signup.php', requestOptions).then(handleResponse);
}
function handleResponse(response) {
return response.text().then(text => {
const data = text && JSON.parse(text);
if (!response.ok) {
if (response.status === 401) {
// auto logout if 401 response returned from api
// logout();
//alert("done");
console.log("done");
}
const error = (data && data.message) || response.statusText;
return Promise.reject(error);
}
return data;
});
}
and in PHP i am calling below code:
header("Content-Type: application/json; charset=UTF-8");
$tempData = html_entity_decode($_POST['text']);
$obj = json_decode($tempData);
//$obj = json_decode($_POST['user'],false);
echo($_POST["user"]);
print_r($obj);
But nothing is printing its showing error"Unexpected token < in JSON at position 0 at JSON.parse ()"
can any one please help me out to resolve this.
use in PHP decode function,like below.Now its working fine:
$formdata = json_decode(file_get_contents('php://input'), true);
$name = $formdata["name"];
$email = $formdata["email"];
$password = $formdata["password"];
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.