将json命令发送到本地应用程序,该应用程序通过javascript从Web浏览器侦听TCP端口5500

时间:2012-08-28 20:45:34

标签: javascript json tcp web

我有一个应用程序,它侦听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>

但它不起作用,

任何想法?

1 个答案:

答案 0 :(得分: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);
});