1

I have that POST method:

function saveSchemaInDatabase(schemaName) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    };

    xhttp.open("POST", "/user/saveSchemaInDatabase", true);
    xhttp.send(schemaName);
}

and i am catching that shoot in my controller in that way:

@PostMapping(path = { "/user/saveSchemaInDatabase" })
public String saveSchemaInDatabase(@RequestBody String schemaName) {

    return "redirect:/user";
}

Can someone tell me how i can send multiple params to that controller? For example i want something like that:

//shoot
function saveSchemaInDatabase(schemaName, diagramJson) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    };

    xhttp.open("POST", "/user/saveSchemaInDatabase", true);
    xhttp.send(schemaName, diagramJson);
}

//catch
@PostMapping(path = { "/user/saveSchemaInDatabase" })
public String saveSchemaInDatabase(@RequestBody String schemaName, @RequestBody String diagramJson) {

    return "redirect:/user";
}

I hope you know what i mean. Of course my way doesn't work. Error 400 appears.

enter image description here

Can someone help me? Im done :(

1 Answer 1

1

You can crete FormData object and add as many values as you want inside it

var data = new FormData();
data.append("email", "[email protected]");
data.append("password", "pistol");

Then send this formData object to the post request

Like this

xhttp.send(data);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but can you tell me what kind of variable type i should put now into my controller to handle that "data" ?
You should parse the data as json object on your controller and then get values from keys that we appended inside data

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.