Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在springboot中将参数从Ajax传递到RestController_Javascript_Ajax_Spring Boot_Spring Restcontroller - Fatal编程技术网

Javascript 如何在springboot中将参数从Ajax传递到RestController

Javascript 如何在springboot中将参数从Ajax传递到RestController,javascript,ajax,spring-boot,spring-restcontroller,Javascript,Ajax,Spring Boot,Spring Restcontroller,我尝试将参数从Ajax传递到RestController以发送电子邮件 这是控制器发送Enail的Post方法 @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody String create(@RequestParam("covere") String covere, @RequestParam("title") Str

我尝试将参数从Ajax传递到RestController以发送电子邮件

这是控制器发送Enail的Post方法

@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";
    }
这就是调用Post并传递变量以发送消息的Ajax

$("#vuta").on("click", function(e) {
    var EmailData = {
              "covere" : "John",
              "title" :"Boston",
              "username" :"test@yahoo.fr",
              "usernameto" :"test@yahoo.fr"
           }

$.ajax({
    type: "POST",
    url: "/emailsend",
    dataType : 'json',
    contentType: 'application/json',
    data: JSON.stringify(EmailData)
});
});
我在发送电子邮件时出现此错误

所需的字符串参数“covere”不正确 “当前”,“路径”:“/emailsend”}


感谢您的帮助

您的控制器希望通过查询字符串获得参数。您可以使用
$.param
将对象格式化为查询字符串并在URL中发送:

$("#vuta").on("click", function(e) {
        var EmailData = {
            "covere" : "John",
            "title" :"Boston",
            "username" :"test@yahoo.fr",
            "usernameto" :"test@yahoo.fr"
        }

        $.ajax({
            type: "POST",
            url: "/emailsend?" + $.param(EmailData),
            dataType : 'json',
            contentType: 'application/json'
        });
});

很乐意帮忙:)