【✅切换到了supabase】Vercel部署前端服务,通过 MongoDB Atlas 的DataAPI服务接口获取数据。提示跨域

Vercel部署前端服务,通过 MongoDB Atlas 的DataAPI服务接口获取数据。结果提示跨域。
但是这个DataAPI服务接口走postman就不会出现跨域提示。

如何在不额外部署后端服务/serverless的情况下解决这个问题呀?

我的前端使用的是axios发起的请求,API模块代码如下
/api/index.ts

import axios from 'axios';

const service = axios.create({
    baseURL: import.meta.env.VITE_MONGODB_SERVER_URL,
    headers: {
        'Content-Type': 'application/json',
        'api-key': import.meta.env.VITE_MONGODB_SERVER_API_KEY
    },
    withCredentials: true, // 跨域请求时是否需要使用凭证
    timeout: 30000, // 请求超时时间
});



export default service;

/api/api.ts

import service from ".";

// 卡片列表
export function getResrouces() {
    return service({
        method: 'POST',
        url: '/v1/action/find',
        data: {
            "dataSource": "xx",
            "database": "xx",
            "collection": "xx",
        }
    })
}

1 个赞

数据库服务切换为supabase服务。
添加数据后,需要做如下的事情

  1. 添加策略 Authentication - Policies

不加策略API返回值永远为空

  1. 设置站点的URL Authentication - URL Configuration

不配置会出现客户端报错,找不到URL

1 个赞

似乎这个论坛没有太多人讨论 supabase

对呀。 个人免费版还是蛮好用的