如何在 jsp 中显示 Json 来自 Restlet Server

How to show Json come from the Restlet Server in jsp?

本文关键字:Json 来自 Restlet Server 显示 jsp      更新时间:2023-09-26

在我的JSP文件中,有一个表单向Restlet服务器发送"POST"请求。然后,Restlet 服务器将返回一个 JsonRepresentation,如何获取 Json 并在我的 JSP 中显示 Json。喜欢这个?但似乎不是工作,为什么?

    <div>
       <form id="simpleForm" method="post" enctype="multipart/form-data">
          <input type="text" name="zi"></input>
          <input type="file" class="file" name="tupian"></input>
          <input type="submit" value="query"></input>
       </form>
     </div>
     <script>
      $("#simpleForm").submit(function(event) {
         alert("success");
         event.preventDefault();//next, I want to post the form on the up to the Reselet Server and deal with the result come from the server,but the server does not work 
         $.post("http://127.0.0.1:9192/CalligraphyWordService",$("#simpleForm").serialize(),function(data) {
         .......
         });
     });
    </script>

您应该使用 ajax 将数据发送到服务器并获得响应,然后使用该响应更新页面

如果你是JavaScript/Ajax的新手,你最好使用像jQuery这样的库。

jQuery中的简单Ajax帖子看起来像这样:

$.post('url/to/your/reslet', function(json_data) {
    var data = $.parseJSON(json_data);
    var xxx = data.xxx; // read property of your json object
    // then you can use xxx to update your page with JavaScript
});

我建议您禁用提交事件的默认操作并使用ajax异步提交表单。因此,当他们 json 响应时,您可以做任何您喜欢的事情来处理它。如果使用 jquery,代码可能如下所示:

$('your_form_id').submit(function(event) {
    event.preventDefault();  // prevent the default action of submit event so that your browser won't be redirect
    $.post('your_Restlet_url', function(data) {
        // update the page using the passed in data
        ...
    });
});