0

I have problems with sending and receiving a list of json objects (jQuery Ajax)

Java Object

public class UserSkill () {

 private long user;
 private long skill;
 private long level;

 //getter and setter methods

}

From Controller i get list of objects and it looks like this

$.getJSON("getList", function(data) {
    console.log(data);
});

//console log ->  [Object { user=4, skill=61, leve=10}, Object { user=3, skill=61, level=20}]

I changed some values and we have following code

ioarray = [];

//update methods

console.log(ioarray);

//console log ->  [Object { user=4, skill=61, level=9000 }, Object { user=3, skill=61, level=100 }]

Ajax POST

$.ajax({
    url : 'goUpdate',
    type : 'POST',
    contentType : 'application/json; charset=utf-8',
    dataType : 'json',
    data: ioarray,
    succcess : function(e) {
        alert('Yeah');
}

Controller

@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
    public Object goUpdatePOST(@RequestBody List<UserSkill> list) {

        //list.get(0).getLevel(); 

        return list;
}

Logs

type Status report

message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource.

Something wrong here ... any ideas?

UPDATE;

Controler

@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
public @ResponseBody String goUpdatePOST(@RequestBody UserSkill[] list) {

    for(UserSkill i : list) {
        System.out.println(i.getSkill());
    }
    return "somepage";

} 

jQuery

var ioarray = [{ user:4, skill:61, level:10}, 
{ user:3,  skill:61, level:20}];

        $.ajax({
            url : 'goUpdate',
            type : 'POST',
            data: JSON.stringify(ioarray),
        });

Console output

JSON
0
    Object { user=4, skill=61, level=10}

1
    Object { user=3, skill=61, level=20}

Source
[{"user":4,"skill":61,"level":10},{"user":3,"skill":61,"level":20}]

to pom.xml inserted jackson-mapper-asl and jackson-core-asl.

Of course this example generate same error... What i am doing wrong? I think I checked every possibility.

6
  • try removing dataType : 'json' Commented Aug 28, 2014 at 11:10
  • No... POST source: undefined=&undefined= Commented Aug 28, 2014 at 11:23
  • try removing @RequestBody then, there is some issue with the data posted and its type. Commented Aug 28, 2014 at 11:25
  • I did like that in data: {list: ioarray}, still occour like above but post at now: list%5B0%5D%5Buser%5D=4&list%5B0%5D%5Bskill%5D=61&list%5B0%5D%5Blevel%5D=10&list%5B1%5D%5Buser%5D=3&list%5B1%5D%5Bskill%5D=61&list%5B1%5D%5Blevel%5D=20 Commented Aug 28, 2014 at 11:28
  • so now your data is getting enocded using UT-8 format, try removing dataType : 'json' Commented Aug 28, 2014 at 11:47

4 Answers 4

2

does this works for you

    $.ajax({
        url : 'goUpdate',
        type : 'POST',
        data: JSON.stringify(ioarray),
        dataType: "json",
        contentType: "application/json"
    });
Sign up to request clarification or add additional context in comments.

5 Comments

it doesn't work too ... I think it probably is not a problem with JSON, but what else?
@user2363971 can you inspect using firebug what exactly is getting posted in data
Accept application/json, text/javascript, /; q=0.01 Accept-Encoding gzip, deflate Accept-Language pl,en-US;q=0.7,en;q=0.3 Content-Length 97 Content-Type application/json; charset=UTF-8 Cookie JSESSIONID=BF78D2145022CEAC47AF35260444E8B2 Host localhost:8069 Referer localhost:8069/xxx/indexpage User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0 X-Requested-With XMLHttpRequest
@user2363971 i am talking about the post request, the json that is being posted
I know it late but the problem was <csrf /> (spring security). Your solution was correct. Thanks for your help.
0

try to use value = "/goUpdate"

@RequestMapping(value = "/goUpdate", method = RequestMethod.POST)

1 Comment

have goUpdate path some prefix? or is running like localhost/goUpdate??
0

I got same problem i didn't find any solution at last i have find another solution using simple HttpServletRequest.
You may use HttpServletRequest to read request data and use JSON API to convert this json String into List<UserSkill> object. You may use GSON API for converting your json string into list .

For this you need following changed in controller class method as shown below :

@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
public @ResponseBody String goUpdatePOST(HttpServletRequest request) 
{
    String jsonStr = IOUtils.toString(request.getInputStream());
    Gson gson = new Gson();
    Type  fooType = new TypeToken<List<UserSkill>>() {}.getType();
    List<UserSkill> list = gson.fromJson(jsonStr,fooType);
    return gson.toJson(list);
}  

In response send JSON string which can be created by converting List<UserSkill> into JSON string using Gson API.

May this will help you.

Comments

0

JS:

 $.ajax({
        type : "post",
        dataType : 'json', 
        url : 'addAdmin',  
        data : JSON.stringify(ioarray)
 })

Controller (note it takes a custom String only). Then it uses Jackson to parse the string manually, because Spring won't do it in this case. (Spring will only auto-parse if you're using @ModelAttribute form binding.)

@PostMapping("/addAdmin")
public boolean addAdmin(@RequestBody String json) throws Exception {

      String decodedJson = java.net.URLDecoder.decode(json, "UTF-8");
      ObjectMapper jacksonObjectMapper = new ObjectMapper(); // This is Jackson
      List<UserRolesGUIBean> userRolesGUIBeans =  jacksonObjectMapper.readValue(
              decodedJson, new TypeReference<List<UserRolesGUIBean>>(){});
      // Now I have my list of beans populated.
}

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.