24

I try to make an AJAX query to my controller in Spring MVC.

My action code is:

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
    //some code    
}

My Ajax query is:

$.ajax({
        type: "POST",
        url:url,
        contentType: "application/json",
        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){
         //here some code
        }
    });

But I got an error:

Required String parameter ''start_date is not present

Why? As I know I presented it like (@RequestParam(value = "start_date") String start_date

UDP
Now I give 404 My class to take data

public class EventData {
    public String end_date;
    public String start_date;
    public String text;
    public String userId;
    //Getters and setters
}

My js AJAX call is:

$.ajax({
    type: "POST",
    url:url,
    contentType: "application/json",
    // data: eventData,
    processData: false,
    data:    JSON.stringify({
        "start_date":   scheduler.getEventStartDate(id),
        "end_date":  scheduler.getEventEndDate(id),
        "text": scheduler.getEventText(id),
        "userId": "1"
    }),

And controller action:

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

And JSON data is:

end_date: "2013-10-03T20:05:00.000Z"
start_date: "2013-10-03T20:00:00.000Z"
text: "gfsgsdgs"
userId: "1"
HBK
  • 735
  • 1
  • 11
  • 29
nabiullinas
  • 1,185
  • 4
  • 20
  • 41

3 Answers3

42

On the server side you expect your request parameters as query strings but on client side you send a json object. To bind a json you will need to create a single class holding all your parameters and use the @RequestBody annotation instead of @RequestParam.

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody CommandBean commandBean){
    //some code
}

Here is a more detailed explanation.

Community
  • 1
  • 1
gadget
  • 1,978
  • 18
  • 21
  • Can you post the actual JSON you are trying to send? You can capture it with Firebug or Chrome/Chromium. – gadget Oct 28 '13 at 13:15
  • 1
    Can you write, how I can see it in Google Chrome? – nabiullinas Oct 28 '13 at 13:26
  • In request payload it shows start_date=Tue+Oct+01+2013+00%3A00%3A00+GMT%2B0400+(Russian+Standard+Time)&end_date=Tue+Oct+01+2013+00%3A05%3A00+GMT%2B0400+(Russian+Standard+Time)&text=New+event&userId= – nabiullinas Oct 28 '13 at 13:29
  • That's weird it seems jquery sends data as query string by default. This is not nice in case of POST requests. Try to add processData: false flag in your .ajax request and wrap your data element in a JSON.stringify({...}) call. like: http://pastebin.com/JLKgLivC to see request payloads in chrome: tools > developer tools > network tab – gadget Oct 28 '13 at 13:58
  • 1
    I found a problem. it was in returning value of controller. I doesn't set JSP page and it returnes 404 always.Your answer was correct – nabiullinas Oct 28 '13 at 16:26
0

I had the same issue.. I solved it by specifying the config params in the post request:

var config = {
    transformRequest : angular.identity,
    headers: { "Content-Type": undefined }
}

$http.post('/getAllData', inputData, *config*).success(function(data,status) {
    $scope.loader.loading = false;
})

config was the parameter I included and it started working.. Hope it helps :)

Rob
  • 26,989
  • 16
  • 82
  • 98
User-8017771
  • 1,512
  • 1
  • 11
  • 17
0

Spring Boot Code

@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
    //some code    
}

Postman Request Link to be send: Add the parameters and value using Parmas in postman and see the below request link.

http://localhost:8080/events/add?start_date=*someValue*&end_date=*someValue*&text=*someValue*&userId=*someValue*