0

Allow me to preface this by saying that I looked at multiple SO posts on this and I am still lost.

So in my php code I am fetching data from my database and then I am trying to insert it into an array as follows:

$arrayResult = array();

        foreach ($result as $item) {
            array_push($arrayResult, array("type" => $item['type'],
                                           "count" => $item['count'])
            );

        }

        echo json_encode($arrayResult);

My problem is as follows, the only time my JS shows any data is when I just print out the data on a successful AJAX call, any attempts at manipulating it fail totally. As in, no data shown at all.

var arrayResult = null;

    $.get("../php/displayGraph.php",

        function (data) {

            arrayResult = (data);
            var result = JSON.parse(arrayResult);

            $("#results").html(arrayResult);
            //$("#results").html(JSON.parse(arrayResult));


        }
    );

The result of this is:

[{"type":"Entertainment","count":"4"},{"type":"Other","count":"31"},{"type":"Politics","count":"50"},{"type":"Sports","count":"3"},{"type":"Technology","count":"9"}]

I am honestly at a loss in terms of what I even need to do to make it work. And here I thought java was bad with json.

5
  • Ok... so you actually parsed the data into JSON and sent it back... but what are you trying to do here? It's not clear at all. Also, jQuery likes it if you pass back an application/javascript header with your JSON. Commented Apr 18, 2017 at 3:19
  • How exactly does it fail when you attempt to manipulate it? Commented Apr 18, 2017 at 3:19
  • So I am trying to make a json object i via php so that I can manipulate it to grab data i need from data server. It fails in that parsing it does nothing, printing the parsed result shows nothing, and if i try to iterate over the result all i see is [Object] in my console.log Commented Apr 18, 2017 at 3:21
  • Uhm... OK, you're 1. Doing AJAX 2. Getting JSON back 3. Parsing JSON into a JS object 4. Trying to shove said object into HTML. #3 and #4 are at odds with each other Commented Apr 18, 2017 at 3:57
  • I am still somewhat of a novice with php and js, why would 3 and 4 be at add? It seems to make sense to me, the php returns an encoded JSON and then we want to parse it and fetch the results from it for use in our JS Commented Apr 18, 2017 at 3:59

2 Answers 2

1

Try like this,

$.get("../php/displayGraph.php",

    function (data) {
        $.each(data, function (i,item){
            console.log(item.type + " === " +item.count);
        } 
        /*arrayResult = (data);
        var result = JSON.parse(arrayResult);*/

        //$("#results").html(arrayResult);
        //$("#results").html(JSON.parse(arrayResult));


    }
);
Sign up to request clarification or add additional context in comments.

7 Comments

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [[{"type":"Entertainment","count":"4"}],[{"type":"Other","count":"31"}],[{"type":"Politics","count":"50"}],[{"type":"Sports","count":"3"}],[{"type":"Technology","count":"9"}]]
It seems you number of dimension is different when compare to json which is in the question. Try like this console.log(item[i].type + " === " +item[i].count);
still the same error, am I correct in assuming that to give the JS the encoded json one uses the following echo: " echo json_encode($arrayResult);"
Try to run displayGraph.php from browser and see the json format. It will help to sort out the problem... Or you can see the json object in the console.
oddly enough, $.get("../php/displayGraph.php", function (data) { var result = JSON.parse(data); $("#results").html(data); console.log(result[1][0].count); } ); works
|
0

Not sure why, but the following works

$.get("../php/displayGraph.php",
        function (data) {
            var result = JSON.parse(data);
            $("#results").html(data);
            console.log(result[1][0].count);
        }
    );

Certainly is a 2D array the way my php makes it, but i did not think this would be how to do as all the other tutorials i saw never had it like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.