【问题标题】:Type Error: Failed to execute 'fetch' on 'Window' when Sending a JWT to server类型错误:向服务器发送 JWT 时无法在“Window”上执行“fetch”
【发布时间】:2021-06-03 10:53:21
【问题描述】:

我在将 JWT 发送到服务器时遇到此问题,服务器看到 JWT,但前端 js 发送此错误并拒绝继续知道我做错了什么,谢谢。

singup.js:127 TypeError: 无法在 'Window' 上执行 'fetch': 提供的值不是类型 '(sequence or record)'

if (e.target.closest('.my-form2')) {
    let x = document.cookie;
    let cookie = x.split('=')
    let JWTs = cookie[1]
    let content = JSON.stringify({ Authentication: `Bearer ${JWTs}` })

    return fetch('/app', {
      method: 'GET',
      //mode: 'no-cors',
      headers: content
    })
      .then(response => response.json())
      .then((user) => {
        console.log(user.name);
        console.log(user.location);
      })
      .catch((error) => {
        console.error(error);
      }).catch(err => {
        if (err === "server") return
        console.log(err)
      })
  }
})

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    headers 应该是一个对象,但它目前是一个字符串,因为您使用let content = JSON.stringify({ Authentication: `Bearer ${JWTs}` })。删除JSON.stringify

    let content = { Authentication: `Bearer ${JWTs}` };

    【讨论】:

    • 听起来服务器返回的响应不是 JSON。
    • 我做到了,但是 SyntaxError: Unexpected token
    • 由于某种原因无法正常工作,我的内容现在看起来如下{授权:“承载 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZC…zA3fQ.AoqBVR65FFUJvwLL942OTcvDoqZrRxfr3cYvQFt1Y0c”}
    • 如果你在返回response.json()之前先console.log(response.text())怎么办?
    猜你喜欢
    • 2022-07-22
    • 2022-11-06
    • 2021-12-20
    • 2019-01-07
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多