首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Roku Roku remote在javascript中

Roku Roku remote在javascript中
EN

Stack Overflow用户
提问于 2020-12-09 17:22:41
回答 1查看 149关注 0票数 1

我得到了我的roku远程控制脚本的网络控制台错误。javascript如下所示:

代码语言:javascript
复制
  <script>
    function rokuSend(RokuAccess) {
      xhr = new XMLHttpRequest(); 
      xhr.onload=function() { alert(xhr.responseText); }
      xhr.open("POST", RokuAccess);
      xhr.send();
    }
    function rokuKeySend(keyVal) {
      rokuSend("http://" + document.getElementById('RokuIP').value + ":8060/keypress/" + keyVal);
    }

我使用以下方式为Roku提供IP地址:

代码语言:javascript
复制
<form id="Roku"><input type="text" id="RokuIP"></form>

当我按下一个键在我的网络上基于远程它发送一个命令,以简化事情,我将使用相同的功能,以发送按键以及其他命令。因此,其中一个名为"rokuKeySend()“的函数只需构造发送键按下发出的命令所需的正确字符串。第二个函数将命令"rokuSend()"sends给Roku。稍后,将发送其他命令从Roku收集数据,因此我将创建更多使用"rokuSend()“的函数。现在,我使用以下按钮(这些按钮稍后将被图像替换),因为它们现在可以很好地作为概念的证明:

代码语言:javascript
复制
<button type="button" onclick="rokuKeySend('Back')">Back</button>

<button type="button" onclick="rokuKeySend('Home')">Home</button>

<button type="button" onclick="rokuKeySend('Up')">Up</button>

<button type="button" onclick="rokuKeySend('Left')">Left</button>

<button type="button" onclick="rokuKeySend('Select')">Select</button></td>

<button type="button" onclick="rokuKeySend('Right')">Right</button>

<button type="button" onclick="rokuKeySend('Down')">Down</button>

<button type="button" onclick="rokuKeySend('InstantReplay')">InstantReplay</button>

<button type="button" onclick="rokuKeySend('Info')">Info</button>

<button type="button" onclick="rokuKeySend('Rev')">Rev</button>

<button type="button" onclick="rokuKeySend('Play')">Play</button>

<button type="button" onclick="rokuKeySend('Fwd')">Fwd</button>

<button type="button" onclick="rokuKeySend('Backspace')">Backspace</button>

<button type="button" onclick="rokuKeySend('Search')">Search</button>

<button type="button" onclick="rokuKeySend('Enter')">Enter</button>

在将IP放入表单的输入后,我可以按任意一个按钮,它们都可以工作。然而,当鼠标点击时,它们会“视觉”地抑制,但不会从抑郁状态返回。在检查浏览器的web控制台时,我发现了一个错误。这个错误很可能是我没有从抑郁状态返回的原因。我该怎么解决这个问题?错误列于下:

代码语言:javascript
复制
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://xxx.xxx.xxx.xxx/keypress/KEY. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

在上面的错误中,'xxx‘表示IP,' key’表示按下的键。

EN

回答 1

Stack Overflow用户

发布于 2020-12-10 21:15:51

它在错误中提到,CORS头丢失了。请求中添加标头"Access-Control-Allow-Origin“需要交叉对象资源共享(CORS)。您可以尝试在请求中添加此标头-

代码语言:javascript
复制
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');

如果它不起作用,您必须创建某种代理解决方案。您可以找到更多关于CORS和修复错误的详细信息这里这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65221761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档