2016-10-11 22 views
2

現在、Angular2フロントエンドはlocalhost:3000で実行され、NodeJSバックエンド(KrakenJSベース)はlocalhost:8000で実行されます。私は資格証明書に入れたとき、私はthis.http.post('http://localhost:8000/login', body, { headers: contentHeaders }) APIを呼び出すが、私はChromeで次の応答を得る:Angular2、NodeJS&Passportの問題

XMLHttpRequest cannot load http://localhost:8000/login. The request was redirected to 'http://localhost:8000/', which is disallowed for cross-origin requests that require preflight. 

しかし、私はthis.http.post('http://localhost:8000/register', body, { headers: contentHeaders })を呼び出すとき、それはすべての詳細情報を格納、PSWDをハッシング(すべての登録魔法を正常に動作していデータベースなど)。ここで

は(register.js)からの私の単純なレジスタのAPIである:ここで

router.post('/', function (req, res) { 

    if (req.body.email && req.body.password) { 
     var User = require('../../models/user'); 

     User.create({ 
      name: req.body.name, 
      email: req.body.email, 
      password: req.body.password 
     }).then(
      function() { 
       res.render('user/registered', {registered: true}); 
      }, 
      function() { 
       res.render('user/registered', {registered: false}); 
      } 
     ); 
    } else { 
     res.render('user/registered', {registered: false}); 
    } 
}); 

は(login.js)からログインAPIです:

router.post('/', function (req, res) { 

    passport.authenticate('local', { 
     successRedirect: req.session.goingTo || '/', 
     failureRedirect: '/login', 
     failureFlash: true 
    })(req, res); 

}); 

答えて

0

は、あなたのメインサーバのファイルでCORSを有効にします

+1

'*'はあなたの 'Access-Control-Allow-Origin'ヘッダ値であってはなりません。それはブラウザのセキュリティ機能の目的を破っています。 –