【问题标题】:I am this Error In Graphql "Unexpected token < in JSON at position 0"我是 Graphql 中的这个错误“位置 0 的 JSON 中的意外令牌 <”
【发布时间】:2022-03-27 02:34:36
【问题描述】:

enter image description here

我正在尝试使用 Graphql 在 Laravel 中创建一个 Api,但我收到此错误“意外的令牌

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: laravel graphql


【解决方案1】:

如果您通过会话(Laravel sanctum)和 CSRF 使用 GraphQL,请在已发布的视图中添加以下内容:

<meta name="csrf-token" content="{{ csrf_token() }}">

修改 Playground 配置:

GraphQLPlayground.init(root, {
  endpoint: "{{url(config('graphql-playground.endpoint'))}}",
  settings: {
    'request.credentials': 'same-origin',
  },
  headers: {
    'X-CSRF-TOKEN': document.querySelector('meta[name="csrftoken"]').content
  }
})

并确保在 config/graphql-playground.php 中取消注释此行:

'middleware' => ['web']

【讨论】:

    猜你喜欢
    • 2016-12-13
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多