graphql - “消息”:“位置 0 的 JSON 中的意外令牌 <”,“堆栈”:“SyntaxError:位置 0 的 JSON 中的意外令牌 <”
问题描述
const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const schema = require('./schema/schema');
const app = express();
app.get('/graphql', graphqlHTTP({
graphiql:true,
schema: schema
}))
app.listen(4000,()=>{
console.log("listining to port 4000..")
})
const graphql = require('graphql');
const{
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql
const UserType = new GraphQLObjectType({
name: 'user',
description: 'Documentation for User...',
fields: ()=>({
id: {type: GraphQLString},
name: {type: GraphQLString},
age: {type: GraphQLInt}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
description: 'description',
fields: {
user: {
type: UserType,
args: {id: {type: GraphQLString}},
resolve(parent,args){
let user = {
id: '345',
age: 34,
name: 'Aman'
}
return user;
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
})
以上是我的代码,我收到此错误:
“消息”:“位置 0 的 JSON 中的意外令牌 <”,
“堆栈”:“SyntaxError:位置 0 的 JSON 中的意外令牌 <”
谁能告诉我如何解决这个错误?提前致谢。
解决方案
这种似乎您在客户端应用程序上看到此错误?附带说明一下,您没有添加太多细节,说明您正在做什么以获取此错误,您在哪里看到它,以及您认为是什么导致它,所以我只能放弃您提供的内容就您的设置而言。
这似乎是您从您期望 JSON 的服务器获得 HTML 响应。通常,使用 GraphQL 可能会发生这种情况,因为您发送的是 GET 请求(这可能会打开 GraphiQL 资源管理器)而不是 POST 请求。
您的 GraphQL 客户端代码应该发出一个 POST 请求,并且您应该确保它发送一个标头来接受application/json
响应。
推荐阅读
- java - 如何在 android studio 中修复 Java 的堆大小问题
- sql - 如果他给了某个帐户,则排除某人(SQL)
- powershell - Get-ACL 的意外输出 | 选择访问
- c++ - QFormLayout 扩展 QGridLayout 的列
- sql - 触发器无效,找不到解决办法
- java - 实现深度优先搜索伪代码的意外行为
- java - 如何读取 Excel XML 电子表格(.XML)中的特定数据列?
- powershell - PowerShell 和 CSV 文件
- c# - 无法调用 Ajax.BeginForm 的 OnFailure 函数
- android - 使用 webview 打开和加密的 url