如何设置nuxtjs代理以避免在连接strapi时出现CORS [英] How to setup nuxtjs proxy to avoid cors when connecting with strapi

查看:34
本文介绍了如何设置nuxtjs代理以避免在连接strapi时出现CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到localhost:1337上的strapi js,但在POST请求中收到CORS错误。我对此进行了大量研究,找到了一些有关如何设置nuxtjs/Proxy以避免此CORS问题的教程。

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

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

和相关的AXIOS标记

axios: { proxy: true },

推荐答案

找到解决方案

只需将以下内容复制粘贴到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
        ...

这篇关于如何设置nuxtjs代理以避免在连接strapi时出现CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆