[英]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.
我有一个通过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.
我从云服务获得一个链接,其中包含json格式的数据。 Now I want this data to be displayed on my Website, but its cross domain and whatever I do it says 401 (Unauthorized).
现在,我希望将这些数据显示在我的网站上,但是它的跨域以及我所做的一切都显示为401(未经授权)。
<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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.