3

I need help, sending data from the server.

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

$sql = "SELECT id, voltage/*, current_out, power ,temperature,  reading_time*/ FROM SensorData GROUP BY id;";



$result = mysqli_query($conn,$sql);
$data1= array();

    
foreach ($result as $row) {
    $data1[] = $row;
}

    $result->close();

shaped

[{"id":"36","voltage":"16"},{"id":"37","voltage":"18"},{"id":"38","voltage":"20"},{"id":"39","voltage":"22"},{"id":"40","voltage":"26"},{"id":"41","voltage":"30"},{"id":"42","voltage":"32"},{"id":"43","voltage":"34"},{"id":"44","voltage":"36"},{"id":"45","voltage":"36"}] 

and I process but I don't know exactly how to approach individual parameters. you don't know where the problem is.

<script >
 
     $(document).ready(function(){
  $.ajax({
    url: "http://192.168.1.16/webput_to_mysql.php",
    method: "GET",
    success: function(data1) {
       console.log(data1);
         var voltage1 = [];
         var id1 = [];
        voltage1.push(data1[1].voltage);
      document.getElementById("demo").innerHTML = voltage1;
    }
  });
});

</script>

enter image description here

1 Answer 1

2

The problem is that the data is coming back as a string, and you're trying to read it as JSON. You can just add the dataType option to ajax, to tell jQuery how to handle things.

$.ajax({
    url: "http://192.168.1.16/webput_to_mysql.php",
    method: "GET",
    dataType: "json",
    ...
});

See the docs for more info.

Another option is to send the json header in PHP (before your echo json_encode...), which should help jQuery detect the type automatically:

header('Content-Type: application/json');

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

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.