0

I'm developing a Raspberry Pi weather station with the intent to display weather measurements using some nice open source gauge tools. The RPi is sending a text file to my ISP server that I use so a php file can parse the data and use which measurements I choose to display with the gauges on a webpage I'm creating. At the end of the php file, I'm using:

echo json_encode($weatherstat);

to encode the data properly. I've successfully set up an AJAX call in my webpage file to execute the php parsing file. I use the javascrip 'alert' command to display the data. The alert does show the data is being transferred.

Here is the javascript code:

wxstats = new Array()
$.ajax({
    url: 'phpParseWxStats1.php',
    datatype: 'json',
    success: function(data) {
        // everything is ok
        alert('AJAX call successful...' + data)
        wxstats = JSON.parse(data);
    },
     error: function() {
        alert('There was some error performing the AJAX call!')
    }
});
dirdeg = wxstats[3];
gust = wxstats[1];
temp = wxstats[20];

The data appearing in the alert window is correct but each data-point is separated by commas. After reading a number of other postings, I tried using the 'wxstats = JSON.parse(data);' line hoping it would parse the data and put it into the wxstats array I declared. It is not working.

Looking for how to correct this and the code to put the string or whatever is being returned into the wxstats array I declared. I'm struggling with this. I've searched many other posting and I'm not finding a solution that fits my requirements. I just need a simple solution!

  • 2
    jQuery's `.ajax()` will parse JSON data into a Javascript object automatically. I suspect you have what you need in `data` already. – Tangentially Perpendicular Jun 29 '21 at 01:38
  • The data being displayed in my alert window is correct. But for some reason, the JSON.parse(data) is not putting the data into the wxstats array which I need. Each value shown in the alert window is separated by a comma. I've tried numerous ways to get this to work but I must be missing some key element here. – HamOp_N8MDP Jun 29 '21 at 02:11
  • Change `datatype` to `dataType` and remove the `JSON.parse()` part. Then follow the advice in the linked duplicate for handling asynchronously loaded data – Phil Jun 29 '21 at 03:42
  • Made the changes as suggested plus one other that I thought was causing the problem. Everything is working perfectly now. Thanks. – HamOp_N8MDP Jun 29 '21 at 21:16

0 Answers0