Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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
Reactjs 在我的头我有令牌,但它说没有令牌,好像没有令牌。但当它在邮递员身上试用时,效果很好。这里有什么问题_Reactjs_Express_Redux_React Redux_Mern - Fatal编程技术网

Reactjs 在我的头我有令牌,但它说没有令牌,好像没有令牌。但当它在邮递员身上试用时,效果很好。这里有什么问题

Reactjs 在我的头我有令牌,但它说没有令牌,好像没有令牌。但当它在邮递员身上试用时,效果很好。这里有什么问题,reactjs,express,redux,react-redux,mern,Reactjs,Express,Redux,React Redux,Mern,这是我的行动代码: export const accept = (clinicianId, duration) => (dispatch, getState) => { axios .post( "http://localhost:5000/api/patient/authAccess", clinicianId, duration, tokenConfig(getState) ) .then(res

这是我的行动代码:

    export const accept = (clinicianId, duration) => (dispatch, getState) => {
  axios
    .post(
      "http://localhost:5000/api/patient/authAccess",
      clinicianId,
      duration,
      tokenConfig(getState)
    )
    .then(res =>
      dispatch({
        type: ACCESS,
        payload: res.data
      })
    )
    .catch(err => {
      dispatch(
        returnErrors(err.response.data, err.response.status, "ACCESS_FAIL")
      );
    });
};
这是我设置令牌并将其添加到标头的方式:

export const tokenConfig = getState => {
const token = getState().auth.token;


const config = {
  headers: {
    "Content-type": "application/json"
  }
};


if (token) {
  config.headers["x-auth-token"] = token;
}

return config;
};
这是我的api,令牌将转到auth函数并授权用户:

  const { clinicianId, duration } = req.body;
  jwt.sign(
    { id: clinicianId },
    config.get("jwtSecretAccess"),
    { expiresIn: duration },
    (err, token) => {
      if (err) throw err;
      res.json({
        token
      });
    }
  );

当我检查令牌的值时,有一个我认为api不能读取它?我不知道现在该怎么办。

您可以使用Axios拦截器,因为您可以为每个http请求添加头