Kau*_*win 0 ajax promise vue.js axios vuex
我正在使用 Nuxtjs 和内置的 Vuex 模块以及 Nuxtjs 的官方 axios。我试图从我的本地服务器获取,但它总是抛出 CORS 错误。
因此,我对 Github 的公共端点进行了 API 调用,但没有成功,仅在我的控制台中收到 CORS 错误。
我正在使用 Store 操作在组件创建生命周期中启动对服务器的 AJAX 调用。这是我的代码。
// component.vue
created () {
this.$store.dispatch('getGithubAPI')
}
// store action
async getGithubAPI ({ commit, state }) {
await this.$axios.$get('https://api.github.com/users/kaungmyatlwin', { headers: { 'Access-Control-Allow-Origin': '*' } })
.then(resp => {
console.log(resp.data)
})
}
Run Code Online (Sandbox Code Playgroud)
仍然没有运气得到它。这是被抛出到控制台的错误消息。
Failed to load https://api.github.com/users/kaungmyatlwin: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我在这里做错了什么?或者它是驻留在 async/await 中的错误?
更新:奇怪的是,网络调用实际上出去并从服务器获取数据,正如在 Chrome 的网络控制台中可以看到的那样。
Kau*_*win 11
好吧,我似乎已经找到了这个问题。
在 中nuxt.config.js,您必须credentials: false允许 CORS 通配符。
我的 axios 配置在这里。
axios: {
baseURL: 'https://api.github.com',
proxyHeaders: false,
credentials: false
}
Run Code Online (Sandbox Code Playgroud)
参考:https : //github.com/nuxt-community/axios-module#credentials
| 归档时间: |
|
| 查看次数: |
24044 次 |
| 最近记录: |