[英]Unable to parse JSON data from server
我在Firefox中收到错误消息:
DataTables警告(表ID ='alertfilters'):DataTables警告:无法解析来自服务器的JSON数据。 这是由JSON格式错误引起的
我正在使用带ajax的DataTables在我的jsp中显示表。
HTML
<table id="alertfilters" class="display" style="width: 100%; border-spacing: 0px;" >
<thead>
<tr>
<th>User Name</th>
<th>Expiration Time</th>
<th>Last Matched Time</th>
<th>State</th>
<th>Matched Today Count</th>
<th>Use RegEx</th>
</tr>
</thead>
</table>
弹簧控制器
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@RequestMapping("/getFilters/{serverName}/")
public JSONObject getFilters(@PathVariable String serverName, HttpServletRequest request) {
JSONObject json = new JSONObject();
List<FilterJSONVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, "");
JSONArray jsonArray = new JSONArray();
jsonArray.addAll(filteredAlerts);
json.put("data", jsonArray);
return json;
}
jQuery的
$(document).ready(function() {
$('#alertfilters').dataTable( {
"sAjaxSource" : 'getFilters/${sessionScope.monitorServerName}/',
"columns": [
{ "data": "userName" },
{ "data": "expirationTime" },
{ "data": "lastMatchedTime" },
{ "data": "state" },
{ "data": "matchedTodayCount" },
{ "data": "useRegEx" }
]
} );
} );
在萤火虫响应中,我可以看到:
{}
&&
{
"data": [{
"activationTime": "1969-12-31T19:00:00.000-0500",
"description": "Set permanent alert filter ",
"expirationTime": "1969-12-31T19:00:00.000-0500",
"hosts": "asdfrd",
"lastMatchedTime": "2012-09-08T10:34:27.501-0400",
"matchStrings": "psl[0-9]",
"matchedTodayCount": "0",
"nameValuePairs": "",
"objectId": "212121",
"state": "PERMANENT",
"useRegEx": "true",
"userName": "z111111z"
}]
}
我的getFilters
方法的return语句的调试屏幕截图。 :
我认为这可能是由于这些{} &&我得到了答复,但我不知道为什么要得到这些。 知道我缺少什么吗?
方法getFilters()不返回JSONObject,因为您没有使用@ResponseBody添加JSONObject。
您需要在方法上方添加@ResponseBody批注,并在@RequestMapping中将响应类型替换为application/json
有关更多详细信息,请参阅本春季文档
要么
您只需在方法上放置@RequestBody批注即可
{} &&
使您的json无效。 这是事实。 您可以尝试调试getFilters
方法并确保json
变量返回正确的json吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.