首页 > 解决方案 > 我设置withCredentials为真,但是Axios中跨站请求失败

问题描述

跨站 url 是可以直接访问的,但我不能通过跨站请求将 cookie 发送到另一个页面。

代码

export const UserLogin = data => {
  return dispatch => {
    dispatch({ type: USER_LOADING, data: true });
    axios
      .post(`${config.apiPath}/api/user/login`, data, { withCredentials: true })
      .then(function(response) {
        localStorage.setItem("user_agency_mgmt", JSON.stringify(response));
        dispatch({ type: USER_LOADING, data: false });
        return dispatch({ type: USER_LOGIN, data: response });
      })
      .catch(function(xhr, status, err) {
        dispatch({ type: USER_LOADING, data: false });
        return dispatch({ type: USER_LOGIN_FAILURE, error: xhr });
      });
  };
};

配置文件

常量 env = process.env.NODE_ENV || “发展”;

const config = {
  development: {
    apiPath: "https://api.realtor.agency21.rocks",
    domain: "localhost",
    revivePath: "https://revive.graana.rocks",
    lockScreen: true
  },
  production: {
    apiPath: "https://api.realtor.agency21.rocks",
    domain: ".agency21.rocks",
    revivePath: "https://revive.graana.rocks",
    lockScreen: true
  },
  domain: ".agency21.rocks"
};
module.exports = config[env];

注意:当我更改apiPathhttp://localhost:3001时工作正常但是当我更改https://api.realtor.agency21.rockscookie 的路径时在跨站点上不起作用

标签: javascriptreactjsecmascript-6

解决方案


推荐阅读