1

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

这是我的ajax函数:

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

只要 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));
            }
        %>

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"}]}

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

4

2 回答 2

0

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

于 2013-05-02T19:31:32.000 回答
0

我知道这个问题很老,你可能早就忘记了,但我遇到了同样的问题,所以我想我会分享我所做的,以防其他人正在寻找解决方案。

只需将 jsp 页面内容类型保留为文本即可。但是当您从响应中获取数据时,请使用 JSON.parse()。

像这样的东西:

JSP

<%@ page contentType="text" %>

*JSON here*

JS

var jsonData = JSON.parse(data);
于 2019-04-12T21:50:34.560 回答