简体   繁体   English

运行此脚本时出现错误代码401,该怎么办?

[英]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.

相关问题 为什么我在发送 post 请求时收到错误代码 401 - Why do I get error code 401 when sending post request (当我有 JavaScript 运行 PHP 脚本时未找到 PHP 文件路径,但出现 ZDA88B4E490C14359B639CZ4A 错误); 该怎么办? - PHP file path not found when I have JavaScript run a PHP script with XMLHTTPREQUEST (404 error); what to do? 我该怎么做才能使这段代码按我的意愿工作 - what can i do to get this piece of code working as i want 当用户关闭选项卡时,我运行什么 JavaScript 代码? - What JavaScript code do I run when the user closes the tab? 使用 OMDB API 时如何修复 401 错误? - How do I fix 401 error when using OMDB API? 我正在尝试使用jquery加载,但是在获取特定代码段时不允许使用脚本标签,该怎么办? - I am trying to use jquery load, but that doesn't allow script tags when getting a certain piece of code, what can I do? 运行 axios 获取请求时收到 400 错误代码? - Getting 400 error code when I run axios get request? 我怎样才能让这个脚本运行在编辑上 - How can i get this Script to run onEdit 我该怎么做才能验证我的代码错误 - What can I do to validate my code error 当我运行此代码时,它只显示 NaN 或不是数字。 我能做些什么来解决这个问题? - When I run this code it just says NaN or not a number. What could I do to fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM