I already looked up questions about this topic but couldn't figure my problem out. I have a php file which contains the array:
$data = ['logged' => $_SESSION['loggedin'], 'sessName' => $_SESSION['name']];
echo json_encode($data);
Here's my AJAX code, but I have no idea what should I put in "data". Basically my goal is to use the $data array in my Javascript code. (So i can manipulate DOM with conditions).
<script>
$.ajax({
type: 'POST',
dataType: "json",
url:'sign-in.php',
data:
success: function(data)
{
try {
data = JSON.parse(data);
}catch(e) {}
console.log(data);
}
});
</script>
datain your ajax success method contains the array. Try, for example,var logged = data.logged;datavariable as it is. When you setdataTypeof the AJAX call, the response value is handled as JSON, and it is parsed for you by jQuery. "dataType "json" : Evaluates the response as JSON and returns a JavaScript object."datais already being supplied to thesuccessfunction. Have you checked what's already indatabefore trying to modify it?data? What did yourconsole.log(data)show?header('Content-type: application/json');echo json_encode($data);