Nel codice jQuery, si dovrebbe avere la proprietà employees
.
data.employees[0].firstName
Quindi sarebbe come questo.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$.getJSON("names.json", function(data) {
console.log(data);
$('body').append(data.employees[0].firstName);
});
</script>
</body>
</html>
Naturalmente avrete bisogno che la proprietà per la versione non jQuery troppo, ma avresti bisogno di analizzare la risposta JSON prima.
Ricorda inoltre che document.write
sta distruggendo l'intera pagina.
Se hai ancora problemi, provare la piena $.ajax
richiesta invece del $.getJSON
involucro.
$.ajax({
url: "names.json",
dataType: "json",
success: function(data) {
console.log(data);
$('body').append(data.employees[0].firstName);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('ERROR', textStatus, errorThrown);
}
});
http://api.jquery.com/jquery.ajax/
fonte
2012-05-07 23:27:02
avevo cambiato la domanda e fissa gli errori. Ora penso che potrebbe ricevere upvotes e sarebbe un buon riferimento ad altre persone. – GarouDan