如何在没有提交表单的情况下使用ajax将数据发送到servlet [英] How to send data to servlet using ajax without a submitting form

查看:145
本文介绍了如何在没有提交表单的情况下使用ajax将数据发送到servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是servlet的新手,我可以从servlet获取数据,但无法向其发送数据,我想在不使用提交表单的情况下执行此操作,我可以获得一些帮助吗

I am new with servlet, I am able to get data from the servlet but not able to send data to it and I want to do this without using a submitting form, can i get some help please

单击按钮它将转到servlet并返回文本而不是发送给它的值

on the click of the button it will go to the servlet and return the text but not the value send to it

这是我的index.jsp

This is my 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>

这个我的servlet

this my 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.


推荐答案

你可以使用$ .ajax()或$ .post在这里。因为你使用了$ .ajax()。请参考以下更正:

you could either use $.ajax() or $.post here. since you have used $.ajax(). please refer below correction:

<!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);
               $('#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);
  }

这篇关于如何在没有提交表单的情况下使用ajax将数据发送到servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆