Ajax POST data in Request Param

13,454

Solution 1

I just did small project with latest version of spring boot and jquery and it is working well, and based on investigation I did I found there are 2 factor can make this issue, one from jquery and other one from Spring MVC converters:

1- jquery ajax have contentType parameter

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

if this one changed to application/json or application/xml will change the way it is sending request to server then will make issue to server parsing, but it is default value will send form as key=value coma separated and this is OK with FormHttpMessageConverter "which is drive us to next point"

2- spring MVC is using FormHttpMessageConverter for "application/x-www-form-urlencoded" parsing or converting then you can use @RequestParam if this converter changed to other converter like:

MappingJackson2HttpMessageConverter for 'application/json'

or

Jaxb2CollectionHttpMessageConverter for 'application/xml'

so it will expect another request and you can get it using @RequestBody

So, you have to check request going from jquery is it form, json, or xml using development tools in your browser, then check you spring code/configuration to be sure that this request is converted by the FormHttpMessageConverter, this converter could be changed by the parameters of your @RequestMapping.

Solution 2

you cant use $.get with payload(data) but you can use $.post. please add attribute contentType in your request parameter.

$.ajax({
    url :"/clientCredentials.json",
    type: "POST",
    contentType: "application/json",
    data: {
        "clientEmail": email,
        "clientName":clientName,
        "orgName":orgName,
        "logoURL":logoURL,
        "redirectURI":redirectUri
    },
    success: function(response){
        alert("sucess");
    },
    error:function(response){
        alert("something went wrong");
    }
});
Share:
13,454
Suraj
Author by

Suraj

http://surajk.info/ | Wrote Hello World the first time when I was 14. Now I can write it in C, C++, Java, Javascript, Python, visual basic, C# and still learning new ways.

Updated on August 23, 2022

Comments

  • Suraj
    Suraj over 1 year

    I am making a POST request as below :

     $.ajax({
         url :"/clientCredentials.json",
         type: "POST",
         data: {
            "clientEmail": email,
            "clientName":clientName,
            "orgName":orgName,
            "logoURL":logoURL,
            "redirectURI":redirectUri
         },
         success: function(response){
            alert("sucess");
    
         },
         error:function(response){
             alert("something went wrong");
         }
     });
    

    On the server, I am using @RequestParams to get this data.

    @RequestParam String clientEmail, @RequestParam String clientName, @RequestParam String orgName, @RequestParam String logoURL, @RequestParam String redirectURI
    

    I am getting below from from server:

    {"code":"400","errorMessage":"Required String parameter 'clientEmail' is not present"}
    

    If I use @RequestBody instead of @RequestParam to accept this data its working fine.

    My question is How can I get this data in Request Parameters? What am I doing wrong? I have tried Jquery($.get(), $.post()) also. nothing working.

    Thanks for any help.