0
クライアントからサーバー側に電子メール値を送信しようとしましたが、いくつかのエラーが発生しています。下の私のコード。クライアントからサーバーに角度2+の値をノードに送信できませんでした
HTML
<form class="example-form" (ngSubmit)="uptpwd()">
<input class="form-control lht" placeholder="Email" type="email" name="email" [(ngModel)]="email">
<br/>
<br/>
<button class="lgbtn" type="submit">Enter</button>
</form>
TS
export class ForgotComponent implements OnInit {
email: string = '';
constructor(private auth: AuthService,
private formBuilder: FormBuilder,
private http: Http,
public toast: ToastComponent) { }
ngOnInit() {
}
uptpwd(email)
{
this.auth.uptpwd(this.email).subscribe();
}
}
サービス
uptpwd(email){
return this.http.post(`http://localhost:3000/sendmail`,email).map(res =>
res.json());
}
サーバー
app.post('/sendmail', function (req, res) {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'f*******@gmail.com',
pass: *************
}
});
let mailOptions = {
from: *******@gmail.com
to:req.body.email,
subject: 'Password Reset',
text: 'Click the link to reset password: ',
};
app.options('/sendmail', function (req, res) {
res.sendStatus(200);
});
res.setHeader('Access-Control-Allow-Origin', 'localhost:4200/forgot'); // Change this to your Angular 2 port number
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', '*');
transporter.sendMail(mailOptions, (error, info) =>
{
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s',
info.messageId, info.response);
});
})
Error: Failed to load resource: the server responded with a status of 500 (Internal Server Error) forgot:1 Failed to load http://localhost:3000/sendmail : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:4200 ' is therefore not allowed access. The response had HTTP status code 500.