Jsp页面没有返回JSON对象

时间:2022-10-24 21:29:36

I was trying to return a JSON Object from this jsp page.But I dont know why Its not providing the required results.Here is my jsp page :

我试图从这个jsp页面返回一个JSON对象。但我不知道为什么它没有提供所需的结果。这是我的jsp页面:

<%@page import="net.sf.json.JSONException"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page contentType="application/json" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="application/json; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%

            JSONObject json      = new JSONObject();
            JSONArray  employeeslist = new JSONArray();
            JSONObject employee;
            try
            {
                int count = 15;

                for (int i=0 ; i<count ; i++)
                {
                    employee = new JSONObject();
                    employee.put("name"     , "Decepticons" + i);
                    employee.put("id"        , "1999" + i);

                    employeeslist.add(employee);
                 }
                json.put("Employeeslist", employeeslist);
            }
                catch (JSONException jse)
                { 

                }

            out.write(json.toString());
    %>
</body>
</html>

Please help me to find error in this code.

请帮我在这段代码中找到错误。

My ajax calling this jsp :

我的ajax称之为jsp:

<script type="text/javascript">
$(document).ready(function() {
    $("input[type=button]").click(function () {
        $.ajax({
        url: 'ValidEmployeeList.jsp',
        dataType: 'json',
        success: function(data) {
            //alert(data);
            alert(JSON.stringify(data));
        },
         error: function() {
            alert('error');
        }
    });

    });
});
</script>

2 个解决方案

#1


0  

You mix Json content type with html format. Try removing html tags at start and end:

您将Json内容类型与html格式混合使用。尝试在开始和结束时删除html标记:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="application/json; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

And

</body>
</html>

Finally do not ever use empty catch block. You might miss some important exception that is real cause.

最后不要使用空挡块。你可能会错过一些真正原因的重要例外。

#2


0  

The json variable is never assigned beyond creating the new, empty, JSONObject. You only use employee and employeeslist. But you never print those out.

除了创建新的空JSONObject之外,永远不会分配json变量。您只使用员工和员工列表。但你永远不会打印出来。

#1


0  

You mix Json content type with html format. Try removing html tags at start and end:

您将Json内容类型与html格式混合使用。尝试在开始和结束时删除html标记:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="application/json; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

And

</body>
</html>

Finally do not ever use empty catch block. You might miss some important exception that is real cause.

最后不要使用空挡块。你可能会错过一些真正原因的重要例外。

#2


0  

The json variable is never assigned beyond creating the new, empty, JSONObject. You only use employee and employeeslist. But you never print those out.

除了创建新的空JSONObject之外,永远不会分配json变量。您只使用员工和员工列表。但你永远不会打印出来。