问题描述
我正在尝试构建一个使用 Windows Device Portal REST APIs 提供某些功能的网页。但是,当我尝试从 JavaScript 代码发出 GET 请求时,出现 CORS 错误 (Request... has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
)。由于标头必须由服务器(即启用了设备门户的 Windows 设备)设置,因此我实际上无法构建任何有用的东西。有什么技巧可以让它发挥作用吗?这是我的代码:
var apiURL = "https://192.168.68.10/api/power/battery";
var username = "abc";
var password = "password123";
var base64Credentials = btoa(username + ":" + password);
var xhr = new XMLHttpRequest();
xhr.open("GET",apiURL,true,);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.setRequestHeader("Authorization","Basic " + base64Credentials);
xhr.send();
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)