Spring 引导发送电子邮件但 Angular 不发送。如何正确连接方法?

Spring boot sends email but Angular does not. How to connect methods correctly?

Spring boot 和 Angular crud 方法都可以,但仍然不清楚如何在 spring boot 和 angular 之间绑定其他方法。

休息API,Spring启动-Angular。正在发送电子邮件。

REST 控制器

@PostMapping("/sendEmail")
public ResponseEntity<String> sendEmail(@PathVariable long id) throws CustomerNotFoundException{
    Customer customer = customerService.findCustomer(id);
    emailService.sendSimpleEmail(customer.getEmail(),
            "Hi dear customer! Your order is " +
                   "ready to pick up","Your order is waiting for you"
            );

    return new ResponseEntity<>("mail sent successfully", HttpStatus.OK);
}

Angular。使用 http.

的服务 class
 sendEmail(id:any):void {
    const url = 'http://localhost:8080/customers/sendEmail';
    this.http.post(url,{responseType: 'text'}).subscribe(
      res=>{location.reload();},
      err=> {
        alert('An error has occurred while sending email')
      }
    )
  }

提前致谢。

假设已配置 CORS,您的端点还需要 id

{id} 添加到您的端点。

@PostMapping("/sendEmail/{id}")
public ResponseEntity<String> sendEmail(@PathVariable long id) throws CustomerNotFoundException{
    Customer customer = customerService.findCustomer(id);
    emailService.sendSimpleEmail(customer.getEmail(),
            "Hi dear customer! Your order is " +
                   "ready to pick up","Your order is waiting for you"
            );

    return new ResponseEntity<>("mail sent successfully", HttpStatus.OK);
}

/ 和参数 id 添加到 url 的末尾。

id = 123;

 sendEmail(id:any):void {
    const url = 'http://localhost:8080/customers/sendEmail/' + id;
    this.http.post(url,{responseType: 'text'}).subscribe(
      res=>{location.reload();},
      err=> {
        alert('An error has occurred while sending email')
      }
    )
  }