vuejs2 - 在 Nuxt.js 中 CORS 阻塞客户端请求

标签 vuejs2 cors vuex nuxt.js

我在提出客户请求时遇到问题。

我遵循了 Nuxt.js 上的文档和 Axios但我似乎仍然无法让它工作。也许我错过了一些东西..

我的 Vue 组件调用了 vuex Action :

methods: {
  open() {
    this.$store.dispatch('events/getEventAlbum');
  }
}

行动 vuex :
export const actions = {
  async getEventAlbum(store) {
    console.log('album action');
    const response = await Axios.get(url + '/photos?&sign=' +   isSigned + '&photo-host=' + photoHost);
    store.commit('storeEventAlbum', response.data.results);
  }
};

还有我的 nuxt.js.config
modules: [
  '@nuxtjs/axios',
  '@nuxtjs/proxy'
],

axios: {
  proxy: true
},

proxy: {
  '/api/': { target: 'https://api.example.com/', pathRewrite: {'^/api/': ''} }
}

任何人都可以提供帮助?

最佳答案

我相信@andrew1325 试图指出的问题是 API 提供商 需要启用 CORS,而不仅仅是您的服务器,在不更改代理中的 header 的情况下,您传递的是相同的 header ,目前阻止访问。

在我看来,你只是失踪了 changeOrigin
请尝试以下配置:

modules: [
  '@nuxtjs/axios',
  '@nuxtjs/proxy'
],

axios: {
  proxy: true
},

proxy: {
  '/api/': { target: 'https://api.example.com/', pathRewrite: {'^/api/': ''}, changeOrigin: true }
}

还要确保您的前端 API url 指向您的代理请求 /api

关于vuejs2 - 在 Nuxt.js 中 CORS 阻塞客户端请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445196/

相关文章:

vue.js - 使用 nuxt auth 模块检测身份验证状态需要刷新

arrays - 如何在 Vuex Store 中管理嵌套数组并通过组件传递它

javascript - VueJS路由自动在url中设置一个 '#'

playframework - Play 框架 CORS 请求

symfony - CORS Api 与 symfony

javascript - 使用大数据过滤列表 - VueJs

javascript - VueJs 运行时组件

javascript - 无法将数据传递到vue模板

vue.js - VueJS List Transitions JavaScript Hooks 并没有被列表中的每个元素调用

javascript - 请求在服务器上成功,但在浏览器中导致 CORS 错误