我是角度和弹簧的新手。我想让用户忘记密码发送邮件重置它。我的问题如何从角度发送到弹簧这些参数:
ModelAndView modelAndView, @RequestParam("email") String userEmail, HttpServletRequest request
这是我的代码angular loginController.js:
$scope.formData = {};
$scope.sentMail = function() {
var data = new FormData();
console.log('data :', data);
data.append('email', $scope.account.mail);
console.log('test 1 :', $scope.account.mail);
console.log("sent mail");
var _url = _contextPath + '/sentEmail';
$http.post(_url, data, {
withCredentials : false,
transformRequest : angular.identity,
headers : {
'Content-Type' : undefined
}
}).success(function (response) {
console.log(response.data);
}).error(function(response){
console.log("Error : "+response.data);
});
}
这是我的春季控制器:
@Autowired
EmailService emailService;
@RequestMapping(value = "/sentEmail", method = RequestMethod.POST)
public ModelAndView processForgotPasswordForm(ModelAndView modelAndView, @RequestParam("email") String userEmail, HttpServletRequest request)
{
User optional= usersService.findByEmail(userEmail);
if(!(optional!= null))
{
modelAndView.addObject("errorMessage", "We didn't find an account for that e-mail address.");
}
else{
String appUrl = request.getScheme() + "://" + request.getServerName();
SimpleMailMessage passwordResetEmail = new SimpleMailMessage();
passwordResetEmail.setFrom("support@demo.com");
passwordResetEmail.setTo(optional.getUserEmail());
passwordResetEmail.setSubject("Password Reset Request");
passwordResetEmail.setText("To reset your password, click the link below:\n" + appUrl);
emailService.sendEmail(passwordResetEmail);
}
modelAndView.setViewName("forgotPassword");
return modelAndView;
}
提前感谢您的帮助。
答案 0 :(得分:1)
我假设您的$ scope变量很好。 你的弹簧控制器似乎很好, 但是你需要在从有角度的方面发送请求时更改你的内容类型,
$http.post(_url, data, {
withCredentials : false,
transformRequest : angular.identity,
headers : {
'Content-Type' : undefined
}
}).success(function (response) {
console.log(response.data);
}).error(function(response){
console.log("Error : "+response.data);
});
而不是Content-type: undefined
,请使用
headers : {
'Content-Type' : 'application/x-www-form-urlencoded'
}
或
headers : {
'Content-Type' : 'application/json'
}
尝试使用此变体
function sendMail($scope) {
$http({
method: 'POST',
url: url,
data: data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
}
也是,而不是这个
var data = new FormData();
console.log('data :', data);
data.append('email', $scope.account.mail);
console.log('test 1 :', $scope.account.mail);
console.log("sent mail");
试
var data = {};
data.email = $scope.account.mail;