Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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 如何通过Apollo客户端传递授权头以在graphql中使用_Reactjs_Graphql_React Apollo_Apollo Client - Fatal编程技术网

Reactjs 如何通过Apollo客户端传递授权头以在graphql中使用

Reactjs 如何通过Apollo客户端传递授权头以在graphql中使用,reactjs,graphql,react-apollo,apollo-client,Reactjs,Graphql,React Apollo,Apollo Client,在我的react应用程序中,我尝试发送一个授权头,其中包含我所做的每个graphql查询/变异 我可以在我的请求中看到标题,如下图所示: 但我得到了401的答复。 当我在postman中运行相同的请求(带有授权头)时,它会工作,并且我会收到作为响应的数据 以下是我的apollo客户端设置: import ApolloClient from 'apollo-client'; import {ApolloProvider} from 'react-apollo'; import { InMemor

在我的react应用程序中,我尝试发送一个授权头,其中包含我所做的每个graphql查询/变异

我可以在我的请求中看到标题,如下图所示:

但我得到了401的答复。 当我在postman中运行相同的请求(带有授权头)时,它会工作,并且我会收到作为响应的数据

以下是我的apollo客户端设置:

import ApolloClient from 'apollo-client';
import {ApolloProvider} from 'react-apollo';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';

const authLink = setContext((_, { headers }) => {

// get the authentication token from redux store.
  const token = store.getState().loginData.jwt;
  // add the jwt token to the authorization header.
  return {
    headers: {
      ...headers,
      Authorization: token ? `Bearer ${token}` : "",
    }
  }
});
const httpLink = new HttpLink({
  uri: process.env.GRAPHQL_API_URL,
  credentials: 'same-origin' 
})

const client = new ApolloClient({
  cache: new InMemoryCache(),
  ssrMode: true,
  connectToDevTools: true,
  link: authLink.concat(httpLink)
});
控制台中没有任何错误。 响应标题为:


如果有任何错误,您能给我们看一下控制台日志吗?还向我们显示响应头