So senden Sie Daten mit Ajax an das Servlet, ohne ein Formular abzusenden

Ich bin neu mit Servlet. Ich kann Daten vom Servlet abrufen, aber keine Daten an das Servlet senden. Ich möchte dies tun, ohne ein übermittelndes Formular zu verwenden. Kann ich bitte Hilfe bekommen?

enn Sie auf die Schaltfläche klicken, wird das Servlet aufgerufen und der Text zurückgegeben, jedoch nicht der Wert, der an das Servlet gesendet wurd

Dies ist meine index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <title>SO question 4112686</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function() {                       
            $('#somebutton').click(function() {               
                $.get('GetUserServlet', function(responseText) { 
                    $('#somediv').text(responseText);        
                });
            });
        });
        $("#somebutton").click(function(){
        $.ajax
        (
        {
            url:'GetUserServlet',
            data:{name:'abc'},
            type:'get',
            cache:false,
            success:function(data){alert(data);},
            error:function(){alert('error');}
        }
    );
}
);
    </script>
</head>
<body>

    <button id="somebutton" onclick="showHint('GetUserServlet.java',   'travis');">press here</button>
    <div id="somediv"></div>
</body>

dieses mein Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "Update Sucessful";
String name = request.getParameter("name");


response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write( name + text);       // Write response body.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage