'Passing object from PHP to Javascipt

I am trying to pass an object from PHP that is created from data returned from database queries. I have followed various tutorials and checked related answers on here but cannot get the JSON to parse without an 'unexpected token' error.

Ideally I would like to preserve the data structure but ultimately I need to end up with a robust solution that can handle hundreds of 'model elements'.

I expect there is something very obvious I am missing but any help would be greatly appreciated.

My PHP file;

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "desman";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

//Get model elements
$sqlElements = "SELECT id, MODEL_ELEMENT_TYPE FROM project_model_elements";
$resultElements = $conn->query($sqlElements);
$modelElements = array();

if ($resultElements->num_rows > 0) {
  while($row = $resultElements->fetch_assoc()) {
    //echo "id: " . $row["id"]. " - Type: " . $row["MODEL_ELEMENT_TYPE"]. "<br>";
    //Create new model element object
    $modelElement = new ModelElement;
    $modelElement->id = $row["id"];
    $modelElement->type = $row["MODEL_ELEMENT_TYPE"];

    //Load points from database
    $points = array();
    $sqlPoints = "SELECT ID,
                  PROJECT_MODEL_ELEMENTS_POINTS_ORDER,
                  PROJECT_MODEL_ELEMENTS_POINTS_X,
                  PROJECT_MODEL_ELEMENTS_POINTS_Y,
                  PROJECT_MODEL_ELEMENTS_POINTS_Z
                  FROM project_model_elements_points
                  WHERE PROJECT_MODEL_ELEMENTS_POINTS_ELEMENTID = ".$row["id"];
    $resultPoints = $conn->query($sqlPoints);
    //Check there are points
    if($resultPoints->num_rows > 0){
      while($pointRow = $resultPoints->fetch_assoc()){
        //Create point
        $elementPoint = new ElementPoint;
        $elementPoint->id = $pointRow["ID"];
        $elementPoint->order = $pointRow["PROJECT_MODEL_ELEMENTS_POINTS_ORDER"];
        $elementPoint->x = $pointRow["PROJECT_MODEL_ELEMENTS_POINTS_X"];
        $elementPoint->y = $pointRow["PROJECT_MODEL_ELEMENTS_POINTS_Y"];
        $elementPoint->z = $pointRow["PROJECT_MODEL_ELEMENTS_POINTS_Z"];
        //Add to points array
        array_push($points, ($elementPoint));
      }
    }
    //Set model elements points
    $modelElement->points = $points;
    array_push($modelElements, ($modelElement));
  }
} else {
  echo "0 results";
}
//Encode model element array
json_encode($modelElements);

$conn->close();

class ModelElement
{
    public $id;
    public $type;
    public $points;
}

class ElementPoint
{
  public $id;
  public $order;
  public $x;
  public $y;
  public $z;
}

From my Javascript;

function reqListener () {
  console.log(this.responseText);
}

var oReq = new XMLHttpRequest(); // New request object
oReq.onload = function() {

  const obj = JSON.parse(this.responseText);
  alert(this.responseText);

  //Loop json object
  //Create objects to be displayed by draw function

};

oReq.open("get", "Database.php", true);

oReq.send();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source