简体   繁体   English

尝试获取JSON对象时,jsp中的参数为null

[英]Parameter is null in jsp when trying to get JSON object

I'm trying to pass a JSON object from jquery to a jsp. 我试图将JSON对象从jquery传递到jsp。 The jquery code is jQuery代码是

$(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: 在JSP方面,我获取对象的代码是:

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

The console just outputs "null". 控制台仅输出“ null”。

Am I missing a step somewhere? 我在某处错过了一步吗?

Because there is no incoming form variable named "formData". 因为没有名为“ formData”的传入表单变量。 Change this line as follows: 更改此行,如下所示:

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

formData is not a parameter. formData不是参数。 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"); 将使用request.getParameter("test");进行检索request.getParameter("test");

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

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