Search code examples
ajaxspring-mvchttp-status-code-400

BadRequest Error in ajax with Spring MVC


I am trying post a request to my controller and everytime when I hit submit button it gives an error of bad request. I am not able to find out what is wrong with the code.

Home.jsp

<div id="setReminder">
                    <label class="generalReminder" style="text-decoration: none;">General
                        Reminder</label>
                    <table>
                        <tr>
                            <td>Date</td>
                            <td><input type="text" readonly="readonly"
                                id="birthdayDate"></td>
                        </tr>
                        <tr>
                            <td>Time</td>
                            <td><input type="text" readonly="readonly" id="callTime"
                                ></td>
                        </tr>
                        <tr>
                            <td>Message</td>
                            <td><textarea id="reminderTag" rows="5"></textarea></td>
                        </tr>
                    </table>
                </div>
                <div id="reminderDot" style="margin-top: 24%; position: relative;">
                    <button class="submitReminder" onclick="saveReminderDetails();">Submit</button>
                </div>

Home.js

function saveReminderDetails(){
var x="";
x=scheduleBirthdayReminder();
if(x){
    $.ajax({
        type:"POST",
        url:"submitBirthdayRequest.do",
        data : {
            birthdayDate :$("#birthdayDate").val(),
            birthdayTime : $("#callTime").val(),
            birthdayReminder : $("#reminderTag").val()
        },
        success : function(data) {
            alert('data is'+data);
            $("#birthdayDate").val('');
            $("#callTime").val('');
            $("#reminderTag").val('');
        }
    });
}

 }

Controller.java

@RequestMapping(value="submitBirthdayRequest.do",method=RequestMethod.POST)
public @ResponseBody String submitSchedulerDetails(@RequestParam("birthdayDate")String birthdayDate,@RequestParam("callTime")String birthdayTime,@RequestParam("reminderTag")String reminderTag,HttpServletRequest request ){
    System.out.println("adding reminder details with birthdayDate "+birthdayDate+"and time"+birthdayTime);
    String userIdentity=((UserDetails)request.getSession(false).getAttribute("loginDetails")).getName();
    try{
        boolean schedulerObj= schedulerService.addSchedulerBirthdayDetails(userIdentity,birthdayDate, birthdayTime,reminderTag);    
    }catch(Exception e){
        e.printStackTrace();
    }
    return birthdayTime;

}

Error

http://localhost:8083/Testing/submitBirthdayRequest.do 400 (Bad Request)

Solution

  • the names of the parameters in the request don't match the expected. Try changing the

    @RequestParam("birthdayTime") String birthdayTime
    @RequestParam("birthdayReminder") String reminderTag
    

    or change the param names on the client side