0
  const client = new ApolloClient({
    uri,
    onError: (e: any) => {
      console.log('error: ', e); // Failed to fetch
      console.log(e.operation.getContext()); // it does show it has x-abc-id
    },

    request: operation => {
      const headers: { [x: string]: string } = {};
      const accessToken = AuthService.getUser()?.accessToken;
      const activeClientId = UserService.getActiveClientId();
      headers['x-abc-id'] = activeClientId;
      if (accessToken) headers['Authorization'] = `Bearer ${accessToken}`;
      operation.setContext({ headers });
    }

});

这里的问题是,当我只添加授权标头时,它会进行 POST 调用并显示预期的错误。

但是,当我添加后端也期望的x-abc-id标头时,它只会进行 OPTIONS 调用(没有后期调用)

PS 在邮递员上添加两个标题完全正常。

4

1 回答 1

0

找到问题所在,如果有帮助,想分享一下。

Postman在向后端发送请求之前不执行OPTIONS 调用。

在 OPTIONS 调用中,表示客户端调用包含的内容:[authorization, content-type, x-abc-id] 在此处输入图像描述

但是服务器期望什么: 在此处输入图像描述

只是授权,内容类型 所以这是一个调用头不匹配(与 Apollo 无关)。

在后端的 CORS 配置中必须明确允许x-abc-id 标头。

感谢Pooria Atarzadeh

于 2020-03-19T08:08:12.950 回答