A Simple AJAX with JSP example

前端 未结 3 987
梦如初夏
梦如初夏 2020-11-30 08:07

I am trying to learn AJAX with JSP and I have written the following code. This does not seem to be working. Kindly help:

This is my configuration_page.jsp

         


        
相关标签:
3条回答
  • 2020-11-30 08:34

    I have used jQuery AJAX to make AJAX requests.

    Check the following code:

    <html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#call').click(function ()
                {
                    $.ajax({
                        type: "post",
                        url: "testme", //this is my servlet
                        data: "input=" +$('#ip').val()+"&output="+$('#op').val(),
                        success: function(msg){      
                                $('#output').append(msg);
                        }
                    });
                });
    
            });
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        input:<input id="ip" type="text" name="" value="" /><br></br>
        output:<input id="op" type="text" name="" value="" /><br></br>
        <input type="button" value="Call Servlet" name="Call Servlet" id="call"/>
        <div id="output"></div>
    </body>
    

    0 讨论(0)
  • 2020-11-30 08:45

    loadXMLDoc JS function should return false, otherwise it will result in postback.

    0 讨论(0)
  • 2020-11-30 08:48

    You are doing mistake in "configuration_page.jsp" file. here in this file , function loadXMLDoc() 's line number 2 should be like this:

    var config=document.getElementsByName('configselect').value;
    

    because you have declared only the name attribute in your <select> tag. So you should get this element by name.

    After correcting this, it will run without any JavaScript error

    0 讨论(0)
提交回复
热议问题