Currently, I am in the process of building a live update chart feature. To access the necessary data, I created a separate file which contains the required information. Through AJAX, I sent a request from my main page to retrieve an Array and incorporate it into the main page. However, I encountered an issue where I cannot utilize this array outside of the request; rather, I can only use it within the <script>
section. Below is what I have implemented:
<script>
$(document).ready(function(){
$.getJSON("loadchart.php", function(data){
var sensor1 = data[0];
var sensor2 = data[1];
var sensor3 = data[2];
var sensorsum = data[3];
});
});
console.log(sensor1);
//The code for my chart goes here
Upon attempting to log the array, an error message is displayed:
Uncaught ReferenceError: sensor1 is not defined
at main.php:107
In an effort to resolve this issue, I came up with the following new approach:
async () => {
async function foo() {
return $.ajax({
url: "loadchart.php",
success: function(data){
return data;
}
});
}
var result = await foo();
console.log(result);
};