简体   繁体   中英

POST without redirecting page

Hi I am writing an application where I want to post data after clicking send button, it will post data to some web-action and gets processed, after processing it will be redirected to some other jsp page but I want to be in the same page from where I click send button. I am not sure of XMLHttpRequest method.

Any ideas to get this done highly appreciated.

If you wanna do using Java script and Ajax (As you have in your question tags) then Try following:

 function fun() {
  if (window.XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
  } else {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  var url = "Serv?req_id=1";
  xmlhttp.open("POST", url, false);
  xmlhttp.send(null);
  var respo= xmlhttp.responseText;
  document.getElementById("some_id").innerHTML = xmlhttp.responseText;
 }

Call fun() on onclick event of send button .

Hope this helps.

您可以使用jQuery.ajax() - 它是XMLHttpRequest的一个方便的包装器。

You can use jQuery. It will save some time. It is cross browser compatible (ie hides the cross browser compatibility techniques from you).

http://api.jquery.com/jQuery.post/

http://jquery.com/

You can issue a 302 request back to the page you came from as the response from the POST request. If you don't want to POST at all I can show you how to do this through AJAX.

The reason you want to use the GET->POST->GET is so that if the user hits the back button it doesn't re-post on you.

Here's a great beginner's tutorial on what you're looking to do (with jQuery):

http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

A 302 request back approach seems to be good for this, If you were using ajax or XMLHttpRequest object, it wouldn't be that problem, but here you can set redirection header to redirect back on the same script that process your query-string.

However you cannot do post without a redirection.

Passing values from one jsp page to another jsp page

I know this is a bit late but for the sake of sharing knowledge, I found a good article here: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ On it, is a very thorough example of how to do what you are asking for.

This is done, as mentioned above, with AJAX/jQuery, this tutorial makes use of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM