I sent JSON object from PHP to JS, and in HTML I get the answer "undefined". Why?
PHP file server.php
$filename = "osoba.json";
$file = file_get_contents($filename);
$json_decoded = json_decode($file);
$json_encoded = json_encode($json_decoded);
echo $json_encoded;
JS file script.js
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var mojNiz = JSON.parse(xhr.responseText);
document.getElementById("demo").innerHTML = mojNiz.ime;
}
};
xhr.open("GET", "server.php", true);
xhr.send();
and HTML index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Pocetna stranica</title
</head>
<body>
<p id="demo">
</p>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
also have a JSON string osoba.json
{"zaposleni":[{"ime":"Dunjica","prezime":"Markovic"},{"ime":"Jovan","prezime":"Markovic"},{"ime":"Marija","prezime":"Markovic"}]}