簡體   English   中英

運行此腳本時出現錯誤代碼401,該怎么辦?

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

我是編程方面的新手。 我有一個通過bluethooth連接到應用程序的傳感器。 該應用程序將數據發送到雲服務。 我從雲服務獲得一個鏈接,其中包含json格式的數據。 現在,我希望將這些數據顯示在我的網站上,但是它的跨域以及我所做的一切都顯示為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM