0

我有一个侦听 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>

但它不起作用,

任何想法?

4

1 回答 1

0

假设您的应用程序已成功发送响应,您需要对其进行处理。尝试这样的事情:

$.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);
});
于 2012-08-28T21:26:49.647 回答