0

I am a newbie in programming. I have a sensor that is connected to the app via bluethooth. The app sends the data to the cloud service. I got a link from the cloud service that contains the data in a json format. Now I want this data to be displayed on my Website, but its cross domain and whatever I do it says 401 (Unauthorized).

<html>
     <head>
      <title>Sensor</title>
      <script src="jquery-3.2.1.min.js"></script>
     </head>
      <body>
       <h1>Sensor</h1>
      <button onclick="myFunctionPost()">Start</button>
      <div id="result" style="color:red"></div>
      <script>
       function myFunctionPost() {
        var getJSON = function(url) {
         return new Promise(function(resolve, reject) {
          var xhr = new XMLHttpRequest();
          xhr.open('get', url, true);
          xhr.responseType = 'json';
          xhr.onload = function() {
           var status = xhr.status;
           if (status == 200) {
            resolve(xhr.response);
           } else {
            reject(status);
           }
          };
          xhr.send();
         });
        };  getJSON('https://thetablewheremydatais$format=json').then(function(data) {
        alert('Your Json result is:  ' + data.result); //you can comment this, i used it to debug

        result.innerText = data.result; //display the result in an HTML element
        }, function(status) { //error detection....
        alert('Something went wrong.');
        });
       }
      </script> 
      </body>
    </html>
Aleksandar K.
  • 13
  • 1
  • 6

1 Answers1

0

Have you tried this line of code before you call the server with xhr.send()

xhr.withCredentials = true;
mrdeadsven
  • 744
  • 1
  • 9
  • 22