0

I'm trying to get 4 JSON arrays that I have in a separate file into my main file using AJAX request. For reason, I can't seem to get the arrays into variables and to display it in console log. This is the results from the JSON file:

[[5,10,10.99,10.99,13,5,14.31,1,1,5,5,5,1,5,3,3,5,5,1,5,10.32,10.32,5,8,5,10,5,5,19,5,7.36,7.36,5,12.2,12.2,2.2,2.2,23.3,5,10.87,6.87,6.87,5,5,10,10,10,10,5,5,5,5,5,0,5,5],

[8,12.5,12.5,12.53,12.53,8,10.11,1,1,8,8,8,1,8,3,3,8,8,1,8,12.83,32.32,8,8,8,10,8.31,8,10,8,18.2,18.2,8,10.3,10.3,2.29,2.29,12.3,8,8.23,2.23,2.23,8,8,10,10,10,20,5,5,5,5,8,0,8,2],

[6,8.86,8.86,8.87,8.87,6,8.33,1,2,6,2,3,1,6,3,8,6,6,1,6,8.32,7.32,6,8,6,10,3.31,6,12,6,12.3,12.3,6,11.1,11.1,4.09,4.09,33.1,6,5.16,12.16,2.16,6,6,10,20,30,30,30,30,5,0,6,0,6,5],

[19,31.36,32.36,32.4,34.4,19,32.76,3,4,19,15,16,3,19,9,14,19,19,3,19,31.47,49.96,19,24,19,30,16.62,19,41,19,37.86,37.86,19,33.6,33.6,8.6,8.6,68.7,19,24.26,21.26,11.26,19,19,30,40,50,60,40,40,15,10,19,0,19,12]]

This is how I tried to get it into my main page:

function callback(response) {
      var array1 = response[0];
      var array2 = response[1];
      var array3 = response[2];
      var array4 = response[3];
      console.log(array1);
}

$.ajax({
  url: 'loadchart.php',
  success: callback
});

The result that I get from trying to get only the all the array1 is only a bracket:

[

Code from loadchart.php:

<?php 

  session_start();

  if(!isset($_SESSION['usersId']))
  {
    header("Location: ../index.php");
    exit();
  }
  else
  {
    include_once 'includes/dbh.inc.php';
  }

  $id = $_SESSION['userId']; 
  $dBname = "infosensor";
  $conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);

  $sql = "SELECT sensor1, sensor2, sensor3 FROM `$id`;";

  $result = mysqli_query($conn, $sql);
  $jsonsensor1 = array();
  $jsonsensor2 = array();
  $jsonsensor3 = array();
  $jsonsensorsum = array();
  if (mysqli_num_rows($result) > 0)
  {
    while ($row = mysqli_fetch_assoc($result))
    {
      $jsonsensor1[] = intval($row['sensor1'] * ($p = pow(10, 2))) / $p;
      $jsonsensor2[] = intval($row['sensor2'] * ($p = pow(10, 2))) / $p;
      $jsonsensor3[] = intval($row['sensor3'] * ($p = pow(10, 2))) / $p;
      $jsonsensorsum[] = intval(($row['sensor1'] + $row['sensor2'] + $row['sensor3']) * ($p = pow(10, 2))) / $p;
      $data = [$jsonsensor1,$jsonsensor2,$jsonsensor3,$jsonsensorsum];
    } 
  }

  echo json_encode($data);
10
  • Will you share the code of loadchart.php? Commented Nov 12, 2019 at 3:41
  • sure, I will edit real quick Commented Nov 12, 2019 at 3:41
  • @pindev there you go :) Commented Nov 12, 2019 at 3:42
  • Did you check network panel of browser developer tools to check whether the API returns correct data? Commented Nov 12, 2019 at 3:44
  • How could I do that? Commented Nov 12, 2019 at 3:45

2 Answers 2

1

Add dataType as json:

$.ajax({
      url: 'loadchart.php',
      dataType:"json",
      success: callback
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Just do one thing user JSON.parse to parse your json array like i have done in your function

function callback(response) {
  var res= JSON.parse(response);
  var array1 = res[0];
  var array2 = res[1];
  var array3 = res[2];
  var array4 = res[3];
  console.log(array1);
}

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.