Announcement Announcement Module
No announcement yet.
Spring MVC REST POST call is giving 400 with JQuery ajax call Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC REST POST call is giving 400 with JQuery ajax call

    I am trying to call my spring MVC REST service and it works with a JQuery $.post but not $.ajax.
    The javascript shows this 400 error;
    1. Failed to load resource: the server responded with a status of 400 (Bad Request)
    1. sendjquery-1.7.2.min.js:4
    2. f.extend.ajaxjquery-1.7.2.min.js:4
    3. logIntoCMS:8080/coco/:124
    4. onclick

    And in spring it gives this error;
    DEBUG: org.springframework.web.servlet.mvc.method.annotat ion.ExceptionHandlerExceptionResolver - Resolving exception from handler [public ng,java.lang.String,java.lang.String,java.lang.Str ing,org.springframework.ui.Model)]: org.springframework.web.bind.MissingServletRequest ParameterException: Required String parameter 'password' is not present

    In JQuery this code works;
    var paramsData = {password:$('password').val(), authorizationType: $('authorizationType').val()};
    $.post(buildURL, paramsData, function(data){ user = data });
    But this doesn't;
                            type: "POST",
                            url: buildURL,
                            data: JSON.stringify({ 
                                password: $('#password').val(), 
                                authorizationType: $('#authorizationType').val() 
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            async: false,
                            success: function(data)
                            { user = data; }
    The spring code is the same for both calls;
        @RequestMapping(value="/{cms}/{username}", method=RequestMethod.POST)
        public @ResponseBody User login(@PathVariable("cms") String cmsName, @PathVariable("username") String username, @RequestParam("password") String password, @RequestParam("authorizationType") String authorizationType, Model model) 
        { never reaches the first line in the method...

    The only real difference is I need it to be synchronous to wait on user data so I need $.ajax with async:false. It looks like the data line is wrong in the ajax call. The data comes from an input field and select box both with the IDs above. Every combination I've tried doesn't work.
    Last edited by mleejr; Apr 2nd, 2013, 02:17 PM.

  • #2
    Ok, I figured this out. Spring is giving a good error. JQuery is sending data. It may not look wrong in a javascript debugger, but it is improperly formatted or something. Remove dataType and contentType. The data now needs to be the format;
    data: { password: $('password').val(), authorizationType: $('authorizationType').val()},