Zeeshan
Zeeshan

Reputation: 12421

Unable to parse JSON data from server

I am getting error in firefox:

DataTables warning (table id = 'alertfilters'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error

I am using DataTables with ajax to display Table in my 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>

Spring Controller

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

In firebug response I can see:

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

Screenshot of the debugging of the return statement of my getFilters method.:

enter image description here

I think this might be because of those {} && I am getting in the response, but I have not idea why I am getting these. Any idea what I am missing?

Upvotes: 2

Views: 132

Answers (2)

Harshal Patil
Harshal Patil

Reputation: 6759

Method getFilters() is not returning a JSONObject because you are not adding JSONObject with @ResponseBody.

You need to add @ResponseBody annotation above the method and prefer response type to application/json in @RequestMapping

For more details refer this spring documentaion

Or

You can do this just by placing @RequestBody annotation over the method

Upvotes: 2

ar4ers
ar4ers

Reputation: 740

{} && makes your json invalid. It's fact. Can you try to debug your getFilters method and make sure that your json variable returns correct json?

Upvotes: 1

Related Questions