如何在不提交表单的情况下使用ajax将数据发送到servlet

如何在不提交表单的情况下使用ajax将数据发送到servlet,第1张

如何在不提交表单的情况下使用ajax将数据发送到servlet

您可以在此处使用$ .ajax()或$ .post。因为您已使用$ .ajax()。请参考以下更正

<!DOCTYPE html><html lang="en"><head>    <title>SO question 4112686</title>    <script src="http://pre.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);    $('#somediv').text(responseText);  }, error:function(){   alert('error'); }         }    );});    </script></head><body>    <button id="somebutton">press here</button>    <div id="somediv"> </div></body>

并且您的servlet应该是:

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.*;import javax.servlet.http.*;public class GetUserServlet extends HttpServlet {  public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {    doPost(request, response);  }  public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {    String text = "Update successfull"; //message you will recieve     String name = request.getParameter("name");    PrintWriter out = response.getWriter();    out.println(name + " " + text);  }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存