Javascript 将JSON数组从PHP传递到Java脚本

Javascript 将JSON数组从PHP传递到Java脚本,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我已经经历了很多解决方案,但是没有在java脚本中获得json,因为我的myfile.php文件中包含了一个错误 <?php ................ print json_encode($data, JSON_NUMERIC_CHECK); mysql_close($con); ?> <html> <head> <script type="text/javascript" src="jquery.min.js">

我已经经历了很多解决方案,但是没有在java脚本中获得json,因为我的myfile.php文件中包含了一个错误

<?php
 ................
print json_encode($data, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
<html>
    <head>
         <script type="text/javascript" src="jquery.min.js"></script>
         <script type="text/javascript" src="example2.js"></script>
        <script>
           var json= loadJsonFromPHP(<?php echo json_encode($data) ?>);
           //var json= <?php echo json_encode($data) ?>;
        </script>
    </head>
    <body></body>
</html>
获取错误无法读取console.log(json)上未定义的的属性“id”,我是PHP新手,不知道哪里出错了

我已尝试在js中使用.getJson,但未定义获取错误ReferenceError:$

function init(){
      // init data
var json=$.getJSON('http://localhost/myfile.php', function(data) {
    console.log(data);
});

}

完全卡住了,需要帮助吗,谢谢。

您的myfile.php应该只包含一个echo json_encode($data)

在“myfile.php”中您可以这样做

<?php
$array = array(
   "success" => true,
   "message" => "Greetings from the Server!"
);
echo json_encode($array);
?>

在您的HTML文件中

<html>
<head>
<script src='js/jquery.js'></script> <!-- your path to jquery goes here -->
</head>
<body>

<div id="target">Message from Server will appear here</div>

<script type="text/javascript">
    // On ready 
    $(function() {
    $.ajax({
        url: 'myfile.php',
        // Before sending the request, show a loading message
        beforeSend: function(){
            $("#target").html("Waiting for response...");
        }
    })
    .done(function(response){
        // Write the response message to the target div
        $("#target").html(response.message);
    });
</script>

<body>
</html>

来自服务器的消息将显示在此处
//准备就绪
$(函数(){
$.ajax({
url:'myfile.php',
//发送请求前,显示加载消息
beforeSend:function(){
$(“#目标”).html(“等待响应…”);
}
})
.完成(功能(响应){
//将响应消息写入目标div
$(“#target”).html(response.message);
});
这真的应该做到


您可以在此处参考
.ajax
的jQuery文档:

在javascript中,您需要解析JSON字符串:

var jsonobj = JSON.parse('<?php echo json_encode($data) ?>');
var jsonobj=JSON.parse(“”);
http://localhost/myfile.php
确保您有:

//You cannot have any echo here you must have clean json-string.
$json = json_encode( $data );
if( json_last_error() === JSON_ERROR_NONE ){
    http_response_code(200);//ok
    header('Content-type: application/json');
    die( $json );
}else{
    http_response_code(500);//server error
    die( 'Cannot encode $data.' );
}

最后,XMLHttpRequest在example2.js中为我解决了问题,希望能有所帮助

request = new XMLHttpRequest( );
 request.open("GET", "my.php", false);
 request.send(null);
  var json = eval ("(" + request.response + ")");

你包括jquery.js了吗?我包括了jquery.min.js文件。我想你必须删除这个函数,保留:var json=;作为常规对象访问json。例如:var l=json.l,var I=json.I,等等……然后你需要
$.parseJSON()
在第二个示例中,因为变量json现在是字符串。@VaheShadunts相同的错误引用error:$未定义,thanksJSON.parse不是jQuery的一部分。您确定jQuery库包含正确吗?
request = new XMLHttpRequest( );
 request.open("GET", "my.php", false);
 request.send(null);
  var json = eval ("(" + request.response + ")");