Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

404
Views
What is the correct way to send a string using AJAX with Spring and Thymeleaf? Status : 400

I am getting error ""Required UsernameSearch parameter 'username' is not present" when sending data using ajax with Spring and Thymeleaf. My script is as shown below

<script>
 $(document).ready(function() {
   $("#opusername").keyup(function() {
  var username = $('#opusername').text();

          $.ajax({
            type : "POST",
            contentType : "application/json",
            url : "/create/check-username-availability",
            data : {"username" : username},
            dataType : 'json',
            timeout : 100000,
            success : function(data) {
              console.log("SUCCESS: ", data);
            },
            error : function(e) {
              console.log("ERROR: ", e);
            },
            done : function(e) {
              console.log("DONE");
            }
          });
      });
  }); 

My controller has this method which should receive the string and return a value to indicate whether the username is available or not.

 @RequestMapping(value = "/create/check-username-availability", method = RequestMethod.POST)
 @ResponseBody
 public String checkUsernameAvailability(@RequestParam("username") UsernameSearch username) {
    logger.info("Checking username availability for username = " + username.getUsername());

    return "true";
}

This is the object I create which contains the username String. I have done this after following tutorials and answers here but nothing seems to work.

public class UsernameSearch {

String username;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

}

This is the error when debugging

error:"Bad Request"
exception:"org.springframework.web.bind.MissingServletRequestParameterException"

message:"Required UsernameSearch parameter 'username' is not present" path:"/create/check-username-availability" status:400 timestamp:1489942516434

I also wanted to ask how to consume the result from the spring method. Is it possible to get a boolean value?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Change @RequestParam to @RequestBody. Request param != body of the request.

The 400 HTTP error means that the the request is malformed. In your case, the controller expects that the url will have a "username" parameter. Therefore you can either change the ajax call, to send the username as the request parameter, i.e. by attaching "?username=fooBar" to the url you're perfoming POST request and change the controller method signature to accept @RequestParam("username") String username, or change the annotation on the controller parameter to @RequestBody, which tells the controller that the data will be sent as an object in the body of the request.

about 4 years ago · Santiago Trujillo Report

0

function ajax:

var data = {
    username: $('#opusername').text()
};
$.ajax({
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    url: "/create/check-username-availability",
    data: JSON.stringify(data), // Note it is important
    success: function (result) {
        // do whatever you want with data
    }
});

and in your java method

@RequestMapping(value = "/create/check-username-availability", method = RequestMethod.POST)
@ResponseBody
public String checkUsernameAvailability(@RequestBody UsernameSearch username) {
    logger.info("Checking username availability for username = " + username.getUsername());

    return "true";
}
about 4 years ago · Santiago Trujillo Report

0

Don't send your data as string! Do the following:

data : {"username" : username},
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!