【问题标题】:How to setup nuxtjs proxy to avoid cors when connecting with strapi与strapi连接时如何设置nuxtjs代理以避免cors
【发布时间】:2021-08-31 13:57:43
【问题描述】:

我正在尝试连接到 localhost:1337 上的strapi js,但我在发布请求时遇到了 cors 错误。我对此进行了很多研究,并找到了一些关于如何设置 nuxtjs/proxy 以避免这个 cors 问题的教程。

以下是我在nuxt.config.js中的相关代理标签

proxy: {
  '/*': {
    target: '[::1]:8080/',
    changeOrigin: true,
    pathRewrite: { '^/*': '' },
  },
},

以及相关的axios标签

axios: { proxy: true },

【问题讨论】:

    标签: nuxt.js strapi


    【解决方案1】:

    找到解决办法

    只需将以下内容复制粘贴到 nuxt.config.js 中

    proxy: {
      '/api': {
        target: 'http://localhost:1337',
        changeOrigin: true,
        pathRewrite: { '^/api': '/' },
      },
    }
    

    并用 /api 附加所有与服务器相关的请求

    例如我的 nuxtjs/auth 代码现在变成了

    auth: {
      strategies: {
        local: {
          endpoints: {
            login: {
              url: '/api/auth/local', //From /auth/local
              method: 'post',
              propertyName: 'jwt',
            user: { url: '/api/session' //From /session
            ...
    

    【讨论】:

      猜你喜欢
      • 2021-05-09
      • 2019-06-25
      • 2022-01-25
      • 2022-12-05
      • 2012-12-08
      • 2022-06-18
      • 2018-03-31
      • 2018-10-28
      • 2016-03-29
      相关资源
      最近更新 更多