小编mic*_*ael的帖子

Axios不会发送cookie,Ajax(xhrFields)也不错

使用Axios

export function sendAll() {
  return (dispatch) => {
    dispatch(requestData());
    return axios({
      method: 'POST',
      url: `${C.API_SERVER.BASEURL}/notification/sendAll`,
      data: {prop: 'val'},
      // responseType: 'json',
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: true
    }).then((response) => {
      dispatch(receiveData(response));
    }).catch((response) => {
      dispatch(receiveError(response));
      // dispatch(pushState(null, '/error'));
    })
  }
};
Run Code Online (Sandbox Code Playgroud)

使用Axios的结果

使用Axios时的Chrome网络标签

使用$ .ajax

$.ajax({
  url: " http://local.example.com:3001/api/notification/sendAll",
  method: "post",
  data: {},
  crossDomain: true,
  xhrFields: {
    withCredentials: true
  }
})
Run Code Online (Sandbox Code Playgroud)

结果使用$ .ajax

使用$ .ajax时的Chrome网络标签

在尝试将数据附加到POST时,我无法强制Axios发送POST(cookie不会以任何方式发送).我的服务器设置(快递):

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", `${C.PROTOCOL}://${C.DOMAIN}:${C.PORT}`);
  res.header("Access-Control-Request-Headers", "*");
  res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", …
Run Code Online (Sandbox Code Playgroud)

javascript ajax axios

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

ajax ×1

axios ×1

javascript ×1