从web浏览器通过javascript向侦听TCP端口5500的本地应用程序发送json命令

Send json command to local application that listens to TCP port 5500 via javascript from web browser

本文关键字:5500 应用程序 命令 json 端口 TCP 浏览器 web javascript      更新时间:2023-09-26

我有一个应用程序,它监听TCP端口号5500,并有一个响应json命令的API。我只想通过JavaScript从web浏览器向应用程序发送json消息,并从应用程序接收json响应。

我曾尝试使用JQuery来做到这一点,如下所示:

<!DOCTYPE html>
<html>
<head>
   <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
   <form action="/" id="searchForm">
     <input type="submit" value="Call" />
   </form>
<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>
<script>
 /* attach a submit handler to the form */
  $("#searchForm").submit(function(event) {
    /* stop form from submitting normally */
    event.preventDefault(); 
   /* get some values from elements on the page: */
   var $form = $( this ),
     term = '{"type" : "command","command" : "call","target" :"demo@vsee.com"}',
     url = "http://localhost:5500";
   /* Send the data using post and put the results in a div */
   $.post( url, term);

});
</script>

但它不起作用,

有什么想法吗?

假设你的应用程序成功发送了响应,你需要对它做点什么

$.post( url, term, function(response){
    // I'm assuming you're getting a plain text response
    // If it's JSON, you'll want to parse it first
    $('#result').text(response);
});