Ich möchte Daten in jQuery zeigen, während die Daten von PHP in Json_encode Funktion erhalten. Es gibt insgesamt drei Dateien. json.htmljQuery Seite zeigen die Daten von json_encode in PHP
<html>
<head>
</head>
<body>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</body>
</html>
$(document).ready(function(){
\t $.getJSON("json.php",function(json){
\t \t
\t \t alert(json.a);
\t \t console.log(json.a);
\t });
});
$(document).ready(function(){
function getA(){
\t $.getJSON("json.php",function(json){
\t \t echo json.$a;
\t })
}
}
<?php
header('Content-type:application/json');
$a=3;
echo json_encode($a);
?>
, wenn ich den Code ausführen, zeigt es wie folgt: test.js: 4SyntaxError: Unerwartete Kennung 'json'
Dank, was Sie sagten, das ehemalige Problem wurde gelöst Aber etwas Unglaubliches passiert. Ich habe test.js wie folgt verändert:
$(document).ready(function(){
\t $.getJSON("json.php",function(json){
\t \t document.write(json.a1.length);
\t \t document.write(json.a.length);
\t }); \t
});
json.php
<?php
// header('Content-type:application/json');
$a = array('a1' =>aaaaa ,'b2'=>bb ,'c3'=>cc);
echo json_encode($a);
?>
Diesmal Browser sagt mir 'a' ist nicht ein Objekt, aber 'a1' ist ein Objekt. Warum wurden die Elemente nicht an js übergeben?
Echo json. $ A // das ist php und nicht javaScript –
Meinst du ich sollte das "echo" entfernen? – zyMacro
ja, und verwenden Sie print, console.log oder Alarm, in JavaScript –