简体   繁体   中英

Parameter is null in jsp when trying to get JSON object

I'm trying to pass a JSON object from jquery to a jsp. The jquery code is

$(document).ready(function(){
        $("form").on("submit", function(event){
            event.preventDefault();

            var formData = JSON.stringify(jQuery("form").serializeArray());
            $.post("<%=request.getRequestURL().toString()%>getInfo.jsp", formData);
    });

});

On the JSP side, my code to get the object is:

out.println(request.getParameter("formData"));

The console just outputs "null".

Am I missing a step somewhere?

Because there is no incoming form variable named "formData". Change this line as follows:

$.post("<%=request.getRequestURL().toString()%>getInfo.jsp", { formData: formData} );

formData is not a parameter. The parameter would be the names of your inputs.

ex

<form>
    <input type="text" name="test"></input>
</form>

Would be retrieved using request.getParameter("test");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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