简体   繁体   中英

I get the Error code 401 when I run this script, what can I do?

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> 

在使用xhr.send()调用服务器之前,您是否尝试过以下代码行?

xhr.withCredentials = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM