0

In my JSP file, there is a Form which send a "POST" request to the Restlet Server. And then, the Restlet Server will return a JsonRepresentation, how to get the Json and show the Json in my JSP.Like this?But is seems not work,why?

    <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>
kerry
  • 1
  • 4

2 Answers2

0

I suggest you disable the default action of the submit event and use ajax to asynchronously submit the form. So when they json response back, you can do whatever you like to deal with the it. If you use jquery, the code might looks like below:

$('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
        ...
    });
});
Cole
  • 179
  • 6
  • Can you give a simple example? – kerry Apr 27 '12 at 08:41
  • Ajax request must follow the so called [SOP(Same Origin Policy)](http://en.wikipedia.org/wiki/Same_origin_policy). That is to say, you jsp file has to be placed under the same domain you would request, that is http://127.0.0.1:9192. Can you check if you satisfy the SOP rule? – Cole Apr 28 '12 at 15:16
  • My JSP file is 127.0.0.1:8080/test/1.jsp. My Restlet Server is running as a OSGI bundle and is not deployed in any web container, such as tomcat.So, I think i can not place my jsp file under 127.0.0.1:9192.My problem is the server can get a post request if i click on the submit but does not show any reaction and doesn't return. It seems the Server cann't receive the form. – kerry Apr 29 '12 at 03:00
  • If 'the sever can get a post request if i click on the submit', how you know 'It seems the Server cant receive the form'? – Cole Apr 29 '12 at 13:51
0

You should use ajax to send your data to the server and get response, then update your page with that response

if you're new to JavaScript/Ajax, you'd better use libraries like jQuery

simple ajax post in jquery looks like this:

$.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
});
wong2
  • 34,358
  • 48
  • 134
  • 179
  • I have updated my question too, can you figure out the problem? – kerry Apr 28 '12 at 11:41
  • @kerry do you want to upload a picture? and what's the url of your jsp page that the form exist? – wong2 Apr 28 '12 at 13:07
  • yes, i want to upload a jpg file. If i don't use the AJAX and javascripte,just a normal html file, it works, but i can't show the json return from the server in my jsp file. – kerry Apr 29 '12 at 03:09
  • @kerry first, you can't use ajax to upload a file, second your jsp page and webservice page have different port(8080 vs 9192), ajax won't work due to the same origin policy – wong2 Apr 29 '12 at 06:48
  • then, how to deal with the peoblem? – kerry Apr 29 '12 at 07:37
  • @kerry you may want to try `uploadify`(http://www.uploadify.com), with jsp: http://blog.sina.com.cn/s/blog_5db0dfe40100idl6.html – wong2 Apr 29 '12 at 10:28