I'm sending a jQuery AJAX request to the server, but the browser's console tells me that the page is not found. Still, my Spring MVC signature mapping the requested URL is executed, and yet the done
part of the AJAX function is not.
我正在向服务器发送jQuery AJAX请求,但浏览器的控制台告诉我找不到该页面。仍然,我的Spring MVC签名映射请求的URL被执行,但AJAX函数的完成部分却没有。
Here is the relevant code:
这是相关的代码:
Javascript:
使用Javascript:
var content = $(data).filter("#wrapper-email-content");
$.ajax({
url : '/sendEmail',
type : 'POST',
data : {
content: content.html()
}
}).done(function(){
console.log("Finished")
});
Spring MVC Signature:
Spring MVC签名:
@RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
public void sendEmail(
HttpServletRequest request, String content) {
UserTO user = (UserTO) request.getSession().getAttribute("USER");
String email = user.getEmail();
String title = "Your order";
Email.sendEmail(title, email, content, null, null, null);
}
So, in the code, the email is sent, meaning that sendEmail method is executed, but I still get the Error 404: not found from the AJAX request, and "Finished" is not printed in the console.
因此,在代码中,电子邮件被发送,这意味着执行了sendEmail方法,但我仍然得到错误404:从AJAX请求中找不到,并且“完成”未在控制台中打印。
What am I doing wrong?
我究竟做错了什么?
3 个解决方案
#1
1
The better solution is annotating your method with
更好的解决方案是使用注释方法
@ResponseStatus(value = HttpStatus.OK)
see answer https://*.com/a/12839817/954602 for the example.
有关示例,请参阅答案https://*.com/a/12839817/954602。
#2
2
@RequestMapping(value = "/sendEmail",headers="Accept=application/json", method = RequestMethod.POST)
public Map<String,String> sendEmail(
HttpServletRequest request, @RequestBody String content) {
Map<String,String> statusMap=new HashMap<String,String>();
UserTO user = (UserTO) request.getSession().getAttribute("USER");
String email = user.getEmail();
String title = "Your order";
Email.sendEmail(title, email, content, null, null, null);
statusMap.put("status","Mail sent successfully");
return statusMap;
}
#3
1
EDIT: see solution posted for What to return if Spring MVC controller method doesn't return value?
编辑:如果Spring MVC控制器方法没有返回值,请参阅发布的解决方案
Not familiar with MVC Spring, but you probably need to return a success status code yourself:
不熟悉MVC Spring,但您可能需要自己返回成功状态代码:
public String sendEmail(
HttpServletRequest request, @RequestBody String content) {
UserTO user = (UserTO) request.getSession().getAttribute("USER");
String email = user.getEmail();
String title = "Your order";
Email.sendEmail(title, email, content, null, null, null);
return new ResponseEntity<String>(json,HttpStatus.OK);
}
#1
1
The better solution is annotating your method with
更好的解决方案是使用注释方法
@ResponseStatus(value = HttpStatus.OK)
see answer https://*.com/a/12839817/954602 for the example.
有关示例,请参阅答案https://*.com/a/12839817/954602。
#2
2
@RequestMapping(value = "/sendEmail",headers="Accept=application/json", method = RequestMethod.POST)
public Map<String,String> sendEmail(
HttpServletRequest request, @RequestBody String content) {
Map<String,String> statusMap=new HashMap<String,String>();
UserTO user = (UserTO) request.getSession().getAttribute("USER");
String email = user.getEmail();
String title = "Your order";
Email.sendEmail(title, email, content, null, null, null);
statusMap.put("status","Mail sent successfully");
return statusMap;
}
#3
1
EDIT: see solution posted for What to return if Spring MVC controller method doesn't return value?
编辑:如果Spring MVC控制器方法没有返回值,请参阅发布的解决方案
Not familiar with MVC Spring, but you probably need to return a success status code yourself:
不熟悉MVC Spring,但您可能需要自己返回成功状态代码:
public String sendEmail(
HttpServletRequest request, @RequestBody String content) {
UserTO user = (UserTO) request.getSession().getAttribute("USER");
String email = user.getEmail();
String title = "Your order";
Email.sendEmail(title, email, content, null, null, null);
return new ResponseEntity<String>(json,HttpStatus.OK);
}