尽管在 vite.config 中为 al 启用了 CORS,但无法访问外部 https 源

问题描述 投票:0回答:1

如果vite有如下配置:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import * as fs from 'fs';

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    proxy: {

    }, 
    cors: {
      origin: '*',
      methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
      preflightContinue: true
    },
    https: {
      key: fs.readFileSync('./.cert/key.pem'),
      cert: fs.readFileSync('./.cert/cert.pem'),
    }
  },
  define: {
    'process.env': {}
  },
  plugins: [react()],
})

还尝试了 ...cors:true... 从我的反应代码中,我尝试访问以下 json api https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0/persoon

这总是会导致 CORS 错误 我可以使用 chrome 和 postman 直接从我的电脑访问此 api。

我用这段 javascript 来调用它作为测试:

 const response = await fetch("https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0/persoon");
 const person = await response.json();
 console.log(person);

有谁知道如何配置服务器和 fetch 或 axios 调用以接受 CORS?

vite httpserver
1个回答
0
投票

事实证明,你不能从 javascript 直接访问这个 api。 我用 C# 创建了一个小型 API 解析器。它从源传递 OData。 这有效。

© www.soinside.com 2019 - 2024. All rights reserved.