简体   繁体   English

Ajax Post请求到Servlet?

[英]Ajax Post request to Servlet?

Hi I have been trying to pass value from ajax to servlet ,have used JQUERY jquery-1.3.2 & query-1.3.2.min .I am successfully able to send the data but if i print the value in servlet getting null 嗨,我一直在尝试将值从ajax传递到servlet,已经使用了JQUERY jquery-1.3.2和query-1.3.2.min。我能够成功发送数据,但是如果我在servlet中打印该值得到null

Here is my js event 这是我的js事件

    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="js/myjquery.js"></script>
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" >


    function AddNext(event)
    {
       MQA.EventManager.addListener(map, 'click', AddNext);
        var lata = event.ll.getLatitude();
        var lnga = event.ll.getLongitude();

        var latlon=lata+"|"+lnga;
        addpasslatlon(latlon);
        MQA.EventManager.clearAllListeners( map);
        exit();
      };   

myjquery.js- myjquery.js-

    function addpasslatlon(latlon) 
    {           
           var value=latlon;
           $.ajax({
             url:'insertPos',
             type:'POST',
             data:value,
             success : function(data){
                 alert('success'+data);
             }
           });
    }

MY servlet class- 我的servlet类-

    public class InsertPos extends HttpServlet {
        private static final long serialVersionUID = 1L;


        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();

            String a =request.getParameter("value");
            System.out.println(a);
        }

    }

web.xml- web.xml-

    <url-pattern>/insertPos</url-pattern>

O/P- null O / P-空

You have to send an object in data: 您必须发送数据对象:

data:{value : value},
//----^^^^^-------------this is the key which you refered in your servlet.

and at your backend you should get it with key value as you are currently doing. 在后端,你应该用钥匙把它value ,你正在做的事情。

You should run a simple test that allows you to see what parameters you have available, you can use getParameterMap() which returns a Map object: 您应该运行一个简单的测试,以查看可用的参数,可以使用getParameterMap()返回一个Map对象:

System.out.println(request.getParameterMap());

this will print out all available parameters for you with key, value pairs of the map, so next when you know all your keys in the map and which values they map to, you can invoke it like you already tried, however this time you will be confident that the value already exists as part of the parameters: 这将为您打印出映射的键,值对的所有可用参数,因此接下来,当您知道映射中的所有键以及它们映射到的值时,可以像尝试过的那样调用它,但是这次您将确信该值已经作为参数的一部分存在:

request.getParameter("<insertKeyHere>");

What Jai did is that he showed you how to pass JSON object to the servlet Jai所做的是,他向您展示了如何将JSON对象传递给Servlet。

data:{value : value}, 数据:{value:value},

This is great for the servlet as it can easily resolve it to a parameter map. 这对于s​​ervlet很有用,因为它可以轻松地将其解析为参数映射。

The problem with this is that you dont want to declare an entire JSON string your self... its fine for one or two values but definitely not for bigger objects. 这样做的问题是您不想自己声明整个JSON字符串...对于一个或两个值很好,但对于较大的对象绝对不是。

What you might want to use is the javascript function that converts all objects to JSON, you could of added this to your ajax call, and it would of worked: 您可能想使用的是将所有对象都转换为JSON的javascript函数,您可以将其添加到ajax调用中,并且可以正常工作:

data: JSON.stringify(value),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM