Warm tip: This article is reproduced from serverfault.com, please click

javascript-网络错误:新ApolloError上位置0的JSON中的意外令牌

(javascript - Network error: Unexpected token < in JSON at position 0 at new ApolloError)

发布于 2018-11-08 14:18:46

在此处输入图片说明

const httpLink = createHttpLink({
  uri: 'http://localhost:3090/'
})

const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache()
})

client.query({
  query: gql`
    query users {
        email
    }
  `,
})
  .then(data => console.log(data))
  .catch(error => console.error(error));

从客户端代码获取时,此查询给出了错误,但是当我在http:// localhost:3090 / graphql的浏览器中执行此查询时,它将正确获取数据

Questioner
Mukesh Kumar
Viewed
0
trixn 2018-11-08 22:51:19

你要向其发布查询的graphql端点缺少/graphql所以,你的服务器可能返回一个包含开头的404错误消息的html文件<<html...Apollo尝试将其解析为查询结果,但没有这样做。

检查httpLink是否确实如此localhost:3090/graphql

查询的语法也可以是:

{
    users {
        email
    }
}

或者,如果你想命名查询:

query Users {
    users {
        email
    }
}