0

I'm trying to make an AJAX call using plain javascript to a Spring controller. The call fails with "Required String parameter 'allowedRoles' is not present"

The controller:

@RequestMapping(path = "/updateRoles", method = RequestMethod.POST)
public String updateRoles(@RequestParam("allowedRoles") String allowedRoles, 
final Map<String, Object> model) {

    return "services";
}

And the AJAX call:

var xhttp;
if (window.XMLHttpRequest) {
    // code for modern browsers
    xhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("POST", "/services/updateRoles", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send({"allowedRoles":allowedRoles});

I have also tried

xhttp.send("allowedRoles=" + allowedRoles);

But the result is the same

2
  • You aren't sending a parameter you are sending a request body. Commented Jul 28, 2017 at 9:15
  • @M.Deinum And how do I send parameters? Commented Jul 28, 2017 at 13:53

1 Answer 1

1

try it with JSON:

//Create JSON data
var jsonData = {allowedRoles: 'string allowed'};
var formattedJsonData = JSON.stringify(jsonData);
//Send it
xhttp.send(formattedJsonData);

If you want, you can check your data to be sure that jsonData is correct, ie:

  console.log(jsonData);
  console.log(JSON.parse(formattedJsonData));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.