2

i try to pass Parameter from Ajax to RestController to send a Email.

That is the Controller Post Methode to send the Enail

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String create(@RequestParam("covere") String covere, @RequestParam("title") String title,
            @RequestParam("username") String username, @RequestParam("usernameto") String usernameto) {
        try {
            mailService.sendMail(covere, title, username, usernameto);
            return "sendmail";
        } catch (MailException e) {
            e.printStackTrace();
        }
        return "sendmail";
    }

That is the Ajax to Call the Post and pass the Variable to send Message

$("#vuta").on("click", function(e) {
    var EmailData = {
              "covere" : "John",
              "title" :"Boston",
              "username" :"[email protected]",
              "usernameto" :"[email protected]"
           }

$.ajax({
    type: "POST",
    url: "/emailsend",
    dataType : 'json',
    contentType: 'application/json',
    data: JSON.stringify(EmailData)
});
});

I have this Error when i send the Email

Required String parameter 'covere' is not present","path":"/emailsend"}

Thank for help

1 Answer 1

2

Your controller is expecting parameters via Query String. You can use $.param to format the object as query string and send it in the URL:

$("#vuta").on("click", function(e) {
        var EmailData = {
            "covere" : "John",
            "title" :"Boston",
            "username" :"[email protected]",
            "usernameto" :"[email protected]"
        }

        $.ajax({
            type: "POST",
            url: "/emailsend?" + $.param(EmailData),
            dataType : 'json',
            contentType: 'application/json'
        });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help :)

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.