开发者

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.


You can use jQuery.ajax() - it's a convenient wrapper for XMLHttpRequest.


You can use jQuery. It will save some time. It is cross browser compatible (i.e. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜