1

I have the following JSON

["2848","241"]

By using following jquery code

var list = [];
    $.each($("input[class='selected']:checked"), function(){
        list.push($(this).val());
    });

I am sending this data to my spring controller by using following in ajax post request.

var jsonData = JSON.stringify(list);

My spring controller is as follows

@RequestMapping(value="/myurl", method =  RequestMethod.POST)
    public String sampleMethod(@RequestBody String jsonData){
        Type dataType = new TypeToken<List<String>>(){}.getType();
        List<String> myListObject = gson.fromJson(jsonData, dataType);
        System.out.println(myListObject.size());
        return "redirect:/someotherurl";
    }

I get 500 internal server error for some reason but no stack trace or errors on my console in browser. Please suggest what is missing.?

10
  • 2
    Check your server logs. You should see a stacktrace there. Commented Apr 16, 2014 at 19:01
  • nothing in my logs, i dont see any stack trace in my eclipse tomcat console either Commented Apr 16, 2014 at 19:49
  • There should always an error if you get an Internal Server Error. If you really can't find anything, set up a breakpoint and step through your sampleMethod code until you find the error. Commented Apr 16, 2014 at 19:58
  • Yes i did that, it always breaks here, List<String> myListObject = gson.fromJson(jsonData, dataType);....and I observed that my "jsonData" variable in the debugger was having some values like %5B% before my actual json string instead of [" , is it normal? Commented Apr 16, 2014 at 20:05
  • If it breaks there, shouldn't you see an error? Are the parameters of fromJson valid and what you'd expect them to be? This method could throw a JsonParseException or a JsonSyntaxException. Commented Apr 16, 2014 at 20:18

1 Answer 1

2

Fixed it by adding contentType: "application/json" to my ajax request. The server was not interpreting the json data properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.