简体   繁体   中英

How to send data from one jsp to another jsp and display data using ajax

I'm very new to ajax and jquery. I have a problem when using jsp and ajax to send data.

I know how to display result (Here I use a table) in the same page using ajax.

Now I want to click a button in the first jsp (the click button uses ajax to call servlet controller so as to get data from database, and then converting the data to json format), then show the result in the second jsp, but I was stuck how to do it.

Here's the code:

test.jsp

 <body>
     <input type='button' value='Show' id='ShowButton' />
 </body>
 <script type='text/javascript'>
    $(document).ready(function() {
        $("#ShowButton").click(function(event) {
            $.ajax({
                type : "POST",
                url : "controller.view",
                dataType : "json",
                success : function(data) {
                    $.each(data, function (index, element) {
                        var showContent = '';
                        showContent += '<tr><td>' + element.cpId + '</td>
                                        <td>' + element.cpName + '</td><td>' 
                                        + element.createDate + '</td><td>' + 
                                        element.enable + '</td></tr>';
                        $("#content tbody").append(showContent);           
                    });
                }
            });
        });
    });
</script>

test2.jsp

<body>
<div >
    <table id='content'>
        <thead>
            <tr>
                <th>ID</th>
                <th>Content Provider Name</th>
                <th>Create Date</th>
                <th>Enable</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>
</body>

Thanks.

//inside button click
$.ajax({
    url: 'SaveData',
    method: 'POST',
    data: {
        fname: $('#fname').val(),
        sname: $('#sname').val(),
        age: $('#age').val(),
        address: $('#address').val(),
        email: $('#email').val(),
        gender: $('.gender:checked').val(),
        phone: $('#phone').val(),
        password: $('#password').val()
    },
    success: function (data) {
        data = JSON.parse(data);
        if (data.responce == 1)
            alert("Success");
        else
            alert("Error");
    },
    error: function (error) {
        console.log(error);
    }
});

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