【问题标题】:$.getJSON data into array$.getJSON 数据放入数组
【发布时间】:2017-09-21 19:06:42
【问题描述】:

我是 PHP 和 JAVASCRIPT 的新手。 我需要做一个小脚本来从 data.php 中获取数据 $.getJSON("data.php", function(json).. 并将其推送到 HTML 文件中的 2 个数组中,以重复使用它们。

data.php 产生这个:

[[47,48,48,48,50,51,48,46,47,45,48,47],[25,23,22,21,19,21,24,25,27,29,31,28]]

现在我这样做了,但它没有运行,我不知道该怎么做。

<!DOCTYPE html>
<html>
<head>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $.getJSON("data.php", function(json) {
            $row1[] = json[0];
            $row2[]= json[1];           

        }); 
    });

</script>
</head>
<body>

<div></div>

</body>
</html>

感谢所有人 ;)

【问题讨论】:

  • 我认为您对 PHP 和 JS 符号感到困惑。 $array[] 表示 append data to an array in PHP,而不是 JavaScript。而object[property]access an object's property value。但是,您的代码中的$row1[] 是无效语法,因为您没有定义任何对象$row1,也没有在[] 中提供任何属性。

标签: javascript php getjson


【解决方案1】:

您的赋值语法错误。在 Javascript 中,您不能将 [] 放在变量的末尾。

$(document).ready(function() {
    $.getJSON("data.php", function(json) {
        var row1 = json[0];
        var row2 = json[1];
        // put code that uses row1 and row2 here
    }); 
});

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多