为什么我的JSON响应会被添加到{}&&

时间:2015-03-12 11:26:43

标签: java jquery ajax json spring

  

为什么我的JSON响应会以 {}&&

为前缀

这是来自Firebug的JSON响应:

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

以下是return语句的eclipse调试截图:

Screenshot of return statement

我的控制器java代码:

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 Call:

 $(document).ready(function() {
    $('#alertfilters').dataTable( {
        "sAjaxSource" : 'getFilters/${sessionScope.monitorServerName}/',
        "columns": [
            { "data": "userName" },
            { "data": "expirationTime" },
            { "data": "lastMatchedTime" },
            { "data": "state" },
            { "data": "matchedTodayCount" },
            { "data": "useRegEx" }
        ]
    } );
} ); 

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

1 个答案:

答案 0 :(得分:1)

修复此问题的方法是在方法上使用@ResponseBody注释。 (org.springframework.web.bind.annotation.ResponseBody)

同时添加

@RequestMapping(“/ getFilters / {serverName} /”,produce =“application / json; charset = utf-8”)