web-dev-qa-db-ja.com

axiosはwithCredentialでもリクエスト付きのCookieを送信できません:true

私はすでにこのようなサーバーにセットアップしました

    app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
  res.header(
    'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization,  X-PINGOTHER'
  );
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');
  next();
});

そしてこのようなクライアント側(反応)のaxios

axios.defaults.withCredentials = true;
axios('http://127.0.0.1:3001/orders', {
  method: 'GET',
  withCredentials: true
}).then(res => {
     console.log(res);
   }).catch(err => {
     console.log(err.response);
   })

Postmanでテストし、直接Chromeに入力すると、すべて正常に機能します。私のコードの何が問題になっているのでしょうか?

2
Huy Nguyen
axios.post('http://localhost:5000/api/auth/login',{ userEmail, userPassword },{
        withCredentials: true,
      })


const cors = require("cors");
expressApplication.use(cors({
 Origin: ["http://localhost:2000", "http://localhost:3000"],
 credentials: true
}));

0

この記事は役に立ちました: https://medium.com/acmvit/handling-cookies-with-axios-872790241a9b

ただし、サーバーの設定によって異なる場合があります。

クライアントセットのAxiosヘッダー:

headers: {Authorization: `Bearer ${cookie_value}`},
withCredentials: true,
crossDomain: true
0
ykwx