我正在尝试使用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函数仍然无法识别它
答案 0 :(得分:0)
根据www.jsonlint.com,每一行都是有效的JSON。另一方面,并不是整个输出有效的json,所以如果你不是逐行喂它可能是原因。
答案 1 :(得分:0)
我知道这个问题很久了,您可能早已忘记了这个问题,但是我遇到了同样的问题,所以我想如果别人在寻找解决方案,我会分享我所做的事情。
只需将jsp页面的内容类型保留为文本。但是,当您从响应中获取数据时,请使用JSON.parse()。
类似这样的东西:
JSP
<%@ page contentType="text" %>
*JSON here*
JS
var jsonData = JSON.parse(data);