JQuery.ajax不会识别JSP的JSON响应

时间:2022-10-07 17:38:32

I am trying to write a simple application using JSP's, Jquery, and AJAX to get and display data on a page.

我正在尝试使用JSP,Jquery和AJAX编写一个简单的应用程序来获取和显示页面上的数据。

Here is my ajax function:

这是我的ajax功能:

    $.ajax({
                type:"POST",
                url:"interface_load_status_page.jsp",
                data:dataString,
                dataType:"json",
                success: function(data, textStatus, jqXHR){
                    $("#response").html("");
                    $("#response").append(data);
                },
            });

That ajax call works properly as long as the dataType is set to "text". Unfortunately I need it to work when it is set to json. Here is the code for the JSP called by the ajax function

只要dataType设置为“text”,ajax调用就能正常工作。不幸的是,当它被设置为json时我需要它才能工作。这是由ajax函数调用的JSP的代码

<%
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

            String date = request.getParameter("date");
            System.out.println("Date: "+date);  
        if (date != null) {
                DBAccess acc = new DBAccess();
                ArrayList<InterfaceLoadStatus> loadStatus = acc
                        .loadStatusQuery(date);
                //System.out.println(loadStatus);
                out.println(Utils.getJSONLoadStatus(loadStatus));
            }
        %>

What that JSP does is take the results of a query to an oracle database and convert it to JSON format like so:

JSP所做的是将查询结果带到oracle数据库并将其转换为JSON格式,如下所示:

{"success":true,"interfaceloadstatus":[{"bType":"COC","start":"2013-04-22 03:30:10.0","end":"2013-04-22 04:35:38.0","exe":"01:05:28","handOff":"Y"},{"bType":"DST","start":"2013-04-22 00:10:09.0","end":"2013-04-22 00:23:17.0","exe":"00:13:08","handOff":"Y"},{"bType":"HTL","start":"2013-04-22 00:10:10.0","end":"2013-04-22 01:17:14.0","exe":"01:07:04","handOff":"Y"},{"bType":"RC","start":"2013-04-22 00:10:11.0","end":"2013-04-22 00:45:17.0","exe":"00:35:06","handOff":"Y"},{"bType":"AIR","start":"2013-04-22 03:00:16.0","end":"2013-04-22 04:03:58.0","exe":"01:04:42","handOff":"Y"},{"bType":"VP","start":"2013-04-22 03:00:16.0","end":"2013-04-22 04:03:58.0","exe":"01:04:42","handOff":"Y"},{"bType":"AIR-IAR","start":"2013-04-22 09:45:06.0","end":"2013-04-22 10:24:51.0","exe":"00:40:45","handOff":"Y"}]}

Ok so now the JSON output is fixed but still the JQuery ajax function does not recognize it

好的,现在JSON输出已修复,但JQuery ajax函数仍无法识别它

1 个解决方案

#1


0  

Each line is valid JSON, according to www.jsonlint.com. On the other hand is not the entire output valid json so if you are not feeding it line by line it might be the reason.

根据www.jsonlint.com,每一行都是有效的JSON。另一方面,并​​不是整个输出有效的json,所以如果你不是逐行喂它可能是原因。

#1


0  

Each line is valid JSON, according to www.jsonlint.com. On the other hand is not the entire output valid json so if you are not feeding it line by line it might be the reason.

根据www.jsonlint.com,每一行都是有效的JSON。另一方面,并​​不是整个输出有效的json,所以如果你不是逐行喂它可能是原因。