2017-12-08 6 views
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.

答えて

0

はこのようにそれをしようと、私は、

app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', '*'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 
を、私はそれが次のように働かせた同じエラーに直面していました
関連する問題