简体   繁体   中英

pass data to spring mvc controller by ajax

How can I normaly pass data to MVc controller action by ajax Now in my js file

$.ajax({
    type: "POST",
    url:url,
    data:     {
            start_date:   scheduler.getEvent(id).start_date,
            end_date:  scheduler.getEvent(id).end_date,
            text: scheduler.getEvent(id).text,
            userId: userId
    },
    success:function(result){
        if(combo.getActualValue() != null){
            getUserEvents(id);
        }
        else{
            $.ajax({
                url:"/WebElanceSh/events",
                success:function(result){
                    json = result;
                    scheduler.parse(json, "json");
                }
            });
        }
    }
});

and in my controller

@RequestMapping(value = "events/add/", method = RequestMethod.POST)
public void addEvent(@RequestBody  String start_date,
                     @RequestBody  String end_date,
                     @RequestBody  String text,
                     @RequestBody  Integer userId){
    Event event = new Event(text,start_date,end_date);

    if(userId == -1){
         TestData.getInstance().AddEvent(-1, event);

    }
    else {
        TestData.getInstance().AddEvent(userId, event);

    }

}

But I have always have Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)

Model an Object that match the ajax request, then use it as request parameter:#

public class MyCommandObject {
   private Date start_date;
   private Date end_date;
   private String text;
   private Integer userId;

   /** Constructor wihtout parameter needed. */
   public MyCommandObject() {}

   Getter and Setter
}


@RequestMapping(value = "events/add/", method = RequestMethod.POST)
public void addEvent(@RequestBody  MyCommandObject command){ ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM