javascript - $.getJSON data into array

254

i'm new in PHP and JAVASCRIPT.. i need to do a little script who takes the data from data.php with$.getJSON("data.php", function(json).. and push it into 2 arrays in the HTML file, to reuse them.

data.php produce this:

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

at now i do this, but it doesn't run and i don't know how to do.

<!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>

thanks to all guys ;)

584

Answer

Solution:

Your assignment syntax is wrong. You can't put[] at the end of a variable in 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
    }); 
});

People are also looking for solutions to the problem: javascript - json_encode of object results in intended callback wrapped in quotes

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.