【问题标题】:to read json array from html file and printing the values in servlet从 html 文件中读取 json 数组并在 servlet 中打印值
【发布时间】:2014-08-14 10:46:23
【问题描述】:

我有一个从表单生成的 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);

【问题讨论】:

    标签: java javascript json servlets


    【解决方案1】:

    你不会通过 request.getParameter("fdata") 得到 fdata

    但您将通过 request.getParameter("your form field name given for fields") 获取所有表单字段值。

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

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

    在 servlet :request.getParameter("您为表单字段指定的表单字段名称")。

    【讨论】:

    • 表单是动态创建的,因此字段名称不是静态的。我需要在 servlet 中打印整个数组。
    • 您可以使用动态字段ID和静态字段名称
    猜你喜欢
    • 2023-04-09
    • 2020-12-03
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2021-04-02
    • 2017-12-30
    相关资源
    最近更新 更多