开发者

Onclick event of a certain page, perform POST action of other page

开发者 https://www.devze.com 2023-01-24 21:52 出处:网络
Let me explain my problem with a scenario: I have web开发者_开发百科pages \"A.jsp\" and \"B.jsp\".

Let me explain my problem with a scenario:

I have web开发者_开发百科pages "A.jsp" and "B.jsp". "B.jsp" is a page with a form that requires input field, 'username' and on submission displays a certain page "C.jsp" with user-specific information. "A.jsp" has table with an element that would correspond to 'username' on "B.jsp". Onclick of a certain 'username' on "A.jsp", I want to be redirected to "C.jsp" with clicked username specific information.

How do I get it done?

I am using MVC framework. Only A.jsp and B.jsp are tied to controller, C.jsp is only a View. But, I believe that is less of an issue, since this seem to be more of a javascript question.

Using prototype javascript framework, I have tried the following:

redirectToC=function(pageurl){

var username=document.getElementById("user1").value;

url=pageurl; //I am using B.jsp as Pageurl

$j.ajax({ type: 'POST',

url: url, data: "username="+username,

success: function(request){

window.location=request;

}

});

}


Does C.jsp support only POST - or does it support GET requests as well?

  1. If it does, in the onClick of the 'username' link, set the location of the browser (i think document.location) to the URL of C.jsp with the username as a GET parameter. Since you are generating the A.jsp page dynamically, this URL can be embedded into the page at generation time
  2. If it does not, a bad hack i can think of is to implement the the table element with the username, as a hidden form element which is submitted via javascript when clicked - 'form'.submit()


<form id="showUserForm" action="/controller/showUser" method="POST">
    <input type="hidden" id="username" name="username">
</form>

..........

<a onclick="showUser('${users.userName}')" href="#"></a>

..........

<script type="text/javascript">
    function showUser(userName){
        document.getElementById("username").value = userName;
        document.getElementById("showUserForm").submit();
    }
</script>
0

精彩评论

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