2017-12-08 1 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(); 
});