Javascript 如何在springboot中将参数从Ajax传递到RestController
我尝试将参数从Ajax传递到RestController以发送电子邮件 这是控制器发送Enail的Post方法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
@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'
});
});
很乐意帮忙:)