【问题标题】:Access to XMLHttpRequest at 'https://***' from origin 'http://localhost:3000' has been blocked by CORS policy从源“http://localhost:3000”访问“https://***”处的 XMLHttpRequest 已被 CORS 策略阻止
【发布时间】:2020-11-11 17:46:31
【问题描述】:

我正在尝试使用 axios 发送请求,但我遇到了 CORS 问题, 这是我的 nuxt.config.js

plugins: [
"@/plugins/axios/apiService.js",
],
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/proxy',
  ],
  
  axios: {
    baseURL: process.env.API_URL,
    proxy: true,
    credentials: false
  },
  proxy: {
    '/api': {
      target: 'https://dev.mobit.ir/api/web/v4',
      pathRewrite: {
        '^/api' : '/'
        },
      changeOrigin: true,
      }
  },

这是我的 apiService

import axios from "axios";
import { API_URL} from "./config";

export const axiosInstance = axios.create({
  headers: {
    Accept: 'application/json',
  }
});

//Set base url for axios requests
axiosInstance.defaults.baseURL = API_URL;

export const ApiService = {
  
  //resource: api address
  get(resource, params = "") {
    return axiosInstance.get(resource, {params}).catch(error => {
      throw new Error(`[RWV] ApiService ${error}`);
    });
  },

  post(resource, params) {
    return axiosInstance.post(`${resource}`, params);
  }
};

我尝试根据 nuxt 文档设置代理,但它不起作用, 如果我使用像https://cors-anywhere.herokuapp.com/ 这样的代理,它会起作用,但为什么 nuxt 代理不起作用?我认为这不是正确的方法。 我必须使用 cookie 或中间件吗?

【问题讨论】:

    标签: vue.js proxy axios nuxt.js


    【解决方案1】:

    根据下面的链接,我们应该使用模块数组中的一个数组来定义代理,如下所示:

    ['@nuxtjs/proxy', { pathRewrite: { '^/api' : '/api/web/v4' } }]

    阅读更多链接: https://github.com/nuxt-community/proxy-module

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2021-03-08
      • 2021-10-05
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2021-12-18
      • 2019-11-22
      • 2022-11-04
      相关资源
      最近更新 更多