用ky获取响应头?

问题描述 投票:0回答:1

我想用vuex在网站上创建身份验证,但是当我发布电子邮件和密码时,响应标题return null

const actions = {
  // Get Token
  async login(context, payload) {
    let data = null
    try {
      data = await http.post(
        process.env.apiUrl + '/api/content/create?type=User',
        {
          body: payload
        }
      )
    } catch (err) {}
    console.log(data.headers.get('Authorization'))
    // context.dispatch('get_user', data.headers.Get('Authorization'))
  },
}

但是在浏览器的网络选项卡中:

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Accept, Authorization, Content-Type
Access-Control-Allow-Origin: *
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3MiOiJjb2xkQGNvbGQuY29sZCIsImF1ZCI6bnVsbCwiZXhwIjoxNTg0NTQxMzkzLCJpYXQiOm51bGwsImlzcyI6bnVsbCwianRpIjpudWxsLCJuYmYiOm51bGwsInN1YiI6bnVsbH0.fIErBy9QklFk4lv73p76ExIpuupuKYkRnDgV0t2L1wk
Cache-Control: max-age=2592000, public
Etag: MTU4MzkzNjUzMw==
Date: Wed, 11 Mar 2020 14:23:14 GMT
Content-Length: 0
post vuex nuxt.js
1个回答
0
投票

您应该在try块中访问参数“ data”。仅当在try块中抛出错误时,catch块才会执行]

const actions = {
 // Get Token
 async login(context, payload) {
let data = null
try {
  data = await http.post(
    process.env.apiUrl + '/api/content/create?type=User',
    {
      body: payload
    }
  )
  context.dispatch('get_user', data.headers.Get('Authorization'))
} catch (err) {}
 console.log(err)

 },
}
© www.soinside.com 2019 - 2024. All rights reserved.