簡體   English   中英

如何將基本身份驗證與 axios nuxt 模塊一起使用

[英]How to use basic auth with axios nuxt module

我目前正在努力使用 Nuxt 的 Axios 模塊: https://axios.nuxtjs.org/ 我想從必須使用基本身份驗證的特定端點獲取一些數據。

通常,使用 Axios,我會執行以下操作:

await axios.get(
  'http://endpoint',
  {},
  {
    withCredentials: true,
    auth: {
      username: 'userame',
      password: 'pw'
    }
  }
)

不幸的是,使用 Nuxt 的 Axios 模塊,似乎並不那么容易......我嘗試了類似的東西:

const data = await this.$axios.$get(
  'http://endpoint',
  {},
  {
    credentials: true,
    auth: {
      username: 'user',
      password: 'pw'
    }
  }
)

但這給我留下了401 Unauthorized ...

我在這里想念什么?

axios.get() (和$axios.$get() )的第二個參數是Axios config ,但您已將其作為第三個參數傳遞(實際上已被忽略)。 API 可能會省略axios.get()中的數據參數,因為數據不適用於此上下文。

解決方案是用您的配置替換第二個參數:

const data = await this.$axios.$get(
  'http://endpoint',
  // {},          // <-- Remove this! 2nd argument is for config
  {
    credentials: true,
    auth: {
      username: 'user',
      password: 'pw'
    }
  }
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM