Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery ajax格式化servlet返回的json响应_Javascript_Jquery_Ajax_Forms_Servlets - Fatal编程技术网

Javascript 如何使用jquery ajax格式化servlet返回的json响应

Javascript 如何使用jquery ajax格式化servlet返回的json响应,javascript,jquery,ajax,forms,servlets,Javascript,Jquery,Ajax,Forms,Servlets,我试图展示使用jqueryajax调用的servlet返回的用户列表。第一个脚本块工作得很好,但无法传递文本和下拉列表值等表单参数,但会在格式良好的列表中显示从服务器返回的用户列表 格式化响应但无法将表单参数传递给servlet的脚本 $(document).ready(function() { $("form").submit(function(){ alert("form submitted"); $.get('SearchUserServlet',

我试图展示使用jqueryajax调用的servlet返回的用户列表。第一个脚本块工作得很好,但无法传递文本和下拉列表值等表单参数,但会在格式良好的列表中显示从服务器返回的用户列表

格式化响应但无法将表单参数传递给servlet的脚本

$(document).ready(function() {
    $("form").submit(function(){
        alert("form submitted");
        $.get('SearchUserServlet', function(responseJson) {
            alert(responseJson);
            alert("inside the servlet");
            var $ul = $('<ul class="list-group">').appendTo($('.well'));
            $.each(responseJson, function(index, item) {
                $('<li class="list-group-item"><strong>').text(item).appendTo($ul);
            });
        });
    });
});
我尝试的第二个脚本将表单参数正确地传递给servlet,但是servlet返回的响应没有像第一个脚本那样格式化。 下面是第二个脚本

var form = $('#SearchForm');

form.submit(function(){
    type: form.attr('method'),url: form.attr('action'),data: form.serialize(),
    success: function(responseJson) {
        alert(responseJson);
        alert("inside the servlet");
        var $ul = $('<ul class="list-group">').appendTo($('.well'));
        $.each(responseJson, function(index, item) {
            $('<li class="list-group-item"><strong>').text(item).appendTo($ul);
        });
    });
});
请帮助我找到这两个脚本中的问题,以便我可以传递表单参数,并在适当的列表中格式化响应。
请检查并给出建议。

假设您的json字符串为jsonString={…,…},请尝试此操作

for(i=0; i<data.jsonString.length; i++) {
    //do your work here with which you are doing inside $.each  
} 

第一次调用,其中数据未传递到服务器;第二次调用,其中表单参数已提交到服务器,但您能否提供第二次调用的输出/json,您将获得未格式化的响应。@TechnoCrat…能否请您简化..无法使senseplease提供json,以便对未格式化异常的原因进行验证。