我在Firefox中收到错误消息:


  DataTables警告(表ID ='alertfilters'):DataTables警告:无法解析来自服务器的JSON数据。这是由JSON格式错误引起的


我正在使用DataTables和ajax在我的jsp中显示Table。

的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

有关更多详细信息,请参见this spring documentaion

要么

您只需在方法上放置@RequestBody批注即可

关于java - 无法解析服务器中的JSON数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29007654/

10-10 23:30