繁体   English   中英

从角度向弹簧发送参数

[英]sending parameters from angular to spring

我是棱角和春季的新手。 如果用户忘记了密码,我想让它发送邮件以重设密码。 我的问题是如何从angular发送到spring这些参数:

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);
    });

}

这是我的春季contoller:

@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;
}

在此先感谢您的帮助。

我假设您的$ scope变量很好。 您的弹簧控制器看起来不错,但是在从有角度的一侧发送请求时,您需要更改Content-type,

$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;

参考来自: https : //www.w3schools.com/angular/angular_http.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM