簡體   English   中英

JavaScript:如何在鍵綁定上發送HTTP發布請求

[英]JavaScript: How to send HTTP post requests on a key-bind

我是HML和JavaScript的新手,想知道如何在按下鍵(例如“ w”)時發送HTTP發布請求,以便控制基於python的機器人。

我看過Google並研究了問題的各個組成部分,但甚至沒有使鍵綁定甚至更改頁面的屬性,包括背景。

我的代碼是當前

<!DOCTYPE html>

<html>
 <head>
  <title>Rover</title>
 </head>
 <body>
  <script>
    $(document).keypress(function(e){
      if(e.shiftKey && e.keyCode === 87){
        alert('UP');
      }
    });
  </script>
  <img src = /stream.mjpg>
 </body>
</html>

其中stream.jpg是來自攝像機的實時供稿。

該代碼應向服務器發送一個HTTP帖子,python將在其中檢測到(服務器使用http.server ),它將控制電動機。

看到您正在使用JQuery,您可以通過keypress()處理程序中的$.post()方法發出POST請求,如下所示:

 <!-- Include JQuery library before script block --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <!-- To host file locally, download the js file above (at cloudflare URL) to the same directory that your HTML is hosted from on your PI, and then use this script block instead (and remove the cloudflare one above) <script src="/jquery.min.js"></script> --> <body> <script> /* Replace server url with actual hostname/ip and port of your python server */ var SERVER_URL = "http://localhost:8080/"; $(document).keypress(function(e) { if (e.shiftKey && e.keyCode === 87) { /* If shift + w key combination, issue POST request to server running at address */ $.post(SERVER_URL); console.log("Shift + w detected"); } }); </script> <img src="/stream.jpg"> </body> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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