我是編程方面的新手。 我有一個通過bluethooth連接到應用程序的傳感器。應用程序將數據發送到雲服務。我從包含json格式數據的雲服務獲取了一個鏈接。現在我希望將這些數據顯示在我的網站上,但是它的跨域和我做的任何事情都說401(未授權)。當我運行這個腳本時,我得到錯誤代碼401,我該怎麼辦?
<html>
\t <head>
\t \t <title>Sensor</title>
\t \t <script src="jquery-3.2.1.min.js"></script>
\t </head>
\t \t <body>
\t <h1>Sensor</h1>
\t \t <button onclick="myFunctionPost()">Start</button>
\t \t <div id="result" style="color:red"></div>
\t \t <script>
\t \t \t function myFunctionPost() {
\t \t \t \t var getJSON = function(url) {
\t \t \t \t \t return new Promise(function(resolve, reject) {
\t \t \t \t \t \t var xhr = new XMLHttpRequest();
\t \t \t \t \t \t xhr.open('get', url, true);
\t \t \t \t \t \t xhr.responseType = 'json';
\t \t \t \t \t \t xhr.onload = function() {
\t \t \t \t \t \t \t var status = xhr.status;
\t \t \t \t \t \t \t if (status == 200) {
\t \t \t \t \t \t \t \t resolve(xhr.response);
\t \t \t \t \t \t \t } else {
\t \t \t \t \t \t \t \t reject(status);
\t \t \t \t \t \t \t }
\t \t \t \t \t \t };
\t \t \t \t \t \t xhr.send();
\t \t \t \t \t });
\t \t \t \t }; \t \t getJSON('https://thetablewheremydatais$format=json').then(function(data) {
\t \t \t \t alert('Your Json result is: ' + data.result); //you can comment this, i used it to debug
\t \t \t \t result.innerText = data.result; //display the result in an HTML element
\t \t \t \t }, function(status) { //error detection....
\t \t \t \t alert('Something went wrong.');
\t \t \t \t });
\t \t \t }
\t \t </script> \t
\t \t </body>
</html>
'401'意味着你必須進行身份驗證。 – sjahan
我知道,但我不知道如何,我試着用client.setRequestHeader(「Authorization」,「Basic」+ btoa(「username:password」)); –
您提到的是基本身份驗證,但也許它不是服務器期望的內容? – sjahan