0

I am trying to return an array object to my Jquery ajax call, but it seems to return as a String with Array length =1

PHP code:

$results = mysqli_query($link, $sql);

if ($results->num_rows > 0) {

    while ($row = $results->fetch_assoc()) {


        $array = array($row['eventDate'], $row['time'] ,['data' => $row['time']])   ;

        echo json_encode($array) ;
    }
} else {
    echo "0 results";
}

Response in Developer tools shows:

["12_03_2015","0:00:00",{"data":"0:00:00"}]["12_03_2015","0:00:00",{"data":"0:00:00"}]["12_03_2015","0:00:00",{"data":"0:00:00"}]

My Ajax Call:

function trend(data){

$.ajax({
    url: "trendData.php",
    cache: true,
    type: "POST",
    data: {arr:data},

    success: function (data) {
        originalData=[];

        originalData.push(data)
        trender(data)
    }
});

}

In the Preview window for Network, it shows an Array:

enter image description here

I cannot get an array of , in this example, 3 objects and get the array.length of 3

Any help will be much appreciated.

enter image description here

5
  • That's an array of 3 elements, what's wrong? It's unclear. Commented Dec 4, 2015 at 16:40
  • When I debug data.length, it returns 1 I for some reason cannot return the array of 3 Commented Dec 4, 2015 at 16:41
  • Because it's an associative array look at starting [[ you should do data[0].length Commented Dec 4, 2015 at 16:42
  • It also looks like the code is printing out 3 separate arrays, as opposed to a collection of all records. How is that not a JSON syntax error I wonder.. Commented Dec 4, 2015 at 16:43
  • data[0].length still shows 1 Commented Dec 4, 2015 at 16:44

2 Answers 2

3

You should collect all your data and return it as one json result and add a correct http-header (suggested by @Flosculus) so that the json format will be recognized:

$results = mysqli_query($link, $sql);
$data = array();

if ($results->num_rows > 0) {
    while ($row = $results->fetch_assoc()) {
        $data[] = array($row['eventDate'], $row['time'] ,['data' => $row['time']])   ;        
    }

    header('Content-Type: application/json');
    echo json_encode($data) ;
} else {
    echo "0 results";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thats good, I'd also change the else statement to something more compatible with Javascript, so if (data.length > 0) {
I have added a screen shot to the original question Still getting length = 1
@user5451365 Try putting header('Content-Type: application/json') at the top of your php file.
That did it. Thank you so much. I had no idea about the header.
0

I think you have to use JSON.parse(), if you're using jquery so your js script will look like

$.ajax({
url: "trendData.php",
cache: true,
type: "POST",
data: {arr:data},

success: function (data) {
    var data = JSON.parse(data);

    //see length of array
    console.log(data.length);
}
});

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.