Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js axios未检索cookie_Node.js_Reactjs_Cors - Fatal编程技术网

Node.js axios未检索cookie

Node.js axios未检索cookie,node.js,reactjs,cors,Node.js,Reactjs,Cors,嘿,我遇到了一个问题,当我在本地运行服务器和应用程序时,没有问题,但是当每个应用程序被推送到各自的服务器时,应用程序不会返回cookie。有人知道怎么避开这件事吗 服务器: app.use(function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Credentials', true); res.header( 'Acc

嘿,我遇到了一个问题,当我在本地运行服务器和应用程序时,没有问题,但是当每个应用程序被推送到各自的服务器时,应用程序不会返回cookie。有人知道怎么避开这件事吗

服务器:

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Credentials', true);
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept'
  );
  next();
});
反应:

const request = axios.post(`${url}/api/login`, {
      email,
      password,
      withCredentials: true,
      headers: { crossDomain: true, 'Content-Type': 'application/json' },
    })
    .then(response => response.data);

尝试使用默认值
axios.defaults.withCredentials=true


这是axios已知的一个问题

我找到了解决方法。我用过:

服务器:

app.use(
  cors({
    credentials: true,
    origin: 'http://myapp.com',
  })
);
反应:

export function loginUser({ email, password }) {
  axios.defaults.withCredentials = true;
  const request = axios
    .post(`${url}/api/login`, {
      email,
      password,
      withCredentials: true,
      headers: { crossDomain: true, 'Content-Type': 'application/json' },
    })
    .then(response => response.data);

  return {
    type: 'USER_LOGIN',
    payload: request,
  };
}

谢谢,但我该把它放在哪里呢?在请求中?把它放在请求之前它对我有效。即使不使用
标题
选项也可以工作。