从HTML文件中读取json数组并在servlet中打印值

to read json array from html file and printing the values in servlet

本文关键字:servlet 打印 json HTML 文件 读取 数组      更新时间:2023-09-26

我有一个JSON数组生成的形式

<script type="text/javascript"> 
        $(function() {
            $('#btn').click(function() {
                var formData=JSON.stringify($('#sform').serializeObject());
                // $('#rValues').text(formData);
                $.get('Partition',"fdata="+formData,function(fJson) {                   
                    $.each(fJson, function(key,value) { 
                        if(fJson!=null){
                        }
                    });
                });
                $("#rValues").show();              
                return false;
            });
        });
        $.fn.serializeObject = function() {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
    </script>

我试图在Partiton.java(servlet)中读取和打印整个表单值,但无法这样做。

 String data = request.getParameter("fdata");
    System.out.println(data);

您将无法通过request.getParameter("fdata")获得fdata

,但是您将根据请求获得所有表单字段值。getParameter("您的表单字段名给出的字段").

在客户端:发送表单数据给servlet

$.ajax({
     url:'servlet',
     data: $("#form1").serialize(),
     success: function (data) {
    }
});

at servlet:请求。