Nuxtjs Axios in Vuex Module CORS error



我正在使用Nuxtjs和内置的Vuex模块以及Nuxtjs的官方axios。我正在尝试从我的本地服务器获取,但它总是抛出 CORS 错误。

因此,我对 Github 的公共端点进行了 API 调用,但仅在控制台中收到 CORS 错误,但没有成功。

我正在使用应用商店操作在组件创建生命周期中启动对服务器的 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)
})
}

仍然没有运气得到它。这是抛出到控制台的错误消息。

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.

我在这里做错了什么?还是驻留在异步/等待中的错误?

更新:奇怪的是,网络调用实际上会出去并从服务器获取数据,正如在Chrome的网络控制台中看到的那样。

好的,我似乎已经解决了这个问题。

nuxt.config.js中,你必须输入credentials: false以允许CORS通配符。

我的公理配置在这里。

axios: {
baseURL: 'https://api.github.com',
proxyHeaders: false,
credentials: false
}

参考: https://github.com/nuxt-community/axios-module#credentials

最新更新