首页 > 其他分享 >simpread-(127 条消息) fastAPI 中的跨域问题解决_小童同学_的博客 - CSDN 博客_fastapi 跨域

simpread-(127 条消息) fastAPI 中的跨域问题解决_小童同学_的博客 - CSDN 博客_fastapi 跨域

时间:2022-11-10 14:12:02浏览次数:72  
标签:http 跨域 origins fastAPI 博客 com localhost backend

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

CrossOrigin

前言

前端采用 Vue,后端采用 fastAPI 的 CV 项目在开发时遇到跨域问题,记录学习过程与解决方案。

概念

  • CORS

CORS or “Cross-Origin Resource Sharing” refers to the situations when a frontend running in a browser has JavaScript code that communicates with a backend, and the backend is in a different “origin” than the frontend.

  • Origin

An origin is the combination of protocol (http, https), domain (myapp.com, localhost, localhost.tiangolo.com), and port (80, 443, 8080).

Then, the browser will send an HTTP OPTIONS request to the backend, and if the backend sends the appropriate headers authorizing the communication from this different origin (http://localhost:8080) then the browser will let the JavaScript in the frontend send its request to the backend.

  • Preflight Request

These are any OPTIONS request with Origin and Access-Control-Request-Method headers.

解决方法

采用 CORS 中间件,后端添加 allowed origins 列表

from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

// 配置允许域名
origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
]
// 配置允许域名列表、允许方法、请求头、cookie等
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

标签:http,跨域,origins,fastAPI,博客,com,localhost,backend
From: https://www.cnblogs.com/zhuoss/p/16876843.html

相关文章

  • simpread-(128 条消息) js 函数柯里化 + class + async await 思想封装我们的 Api_fro
    柯里化所谓"柯里化",就是把一个多参数的函数,转化为单参数函数在项目中,例如定义了如下请求接口的方法:类中也是可以使用async和await1.柯里化封装之前//a.js文......
  • springBoot实现全局跨域
    importlombok.extern.slf4j.Slf4j;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.s......
  • 博客马拉松|和 OpenMLDB 一路向前
    在10月,OpenMLDB与JinaAI、太极图形、OpenPPL、电鸭社区、LadiesWhoTech、云启资本等合作伙伴,举办围绕开发者文化的1024嘉年华活动,一起做有意思的活动,发现有意思......
  • [Kyana]静态博客的使用
    00|前排提示静态博客好,问题真不少。常见静态站点生成器有Hexo(JS)、Hugo(golang)、Jekyll(Ruby),其中Hexo最常见、主题和插件都十分丰富;Jekyll是GitHub使用的站点生成器。......
  • django-cors-headers实现跨域的方法总结
    django-cors-headers实现跨域的方法总结在setting.py中配置INSTALLED_APPS=['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.cont......
  • 【luffy】前台全局样式、后台主页模块接口、跨域问题、自定义配置、git介绍和安装
    目录1.前台全局样式和js配置1.1bodydiv默认样式,统一去掉1.2全局配置2.后台主页模块接口2.1根据原型图分析出来2.2软件开发模式2.3轮播图表2.4轮播图接口编写3.......
  • CakePHP 2.x十分钟博客教程(二):控制器、模型与视图
     在上篇​​CakePHP教程​​中,为大家介绍了CakePHP的安装与配置过程。你的CakePHP框架现在应该已经能够建立应用程序了,本文为大家带来CakePHP如何创建控制器、模型及视图文......
  • 博客园美化教程指引
    展示博客园首页美化效果    展示博客园文章美化效果  简单介绍: 本美化是基于博客园皮肤==> SimpleMemory所进行的,以下会给出可靠的指引,根据指引完成......
  • Github 静态博客搭建之绑定域名
    购买域名本人是在腾讯云买的,按照官网流程购买并实名认证即可,完成后的界面如下。配置域名解析鼠标悬停至DNSPod,出现一个对话框,点击“管理解析”跳转至管理界面。管理......
  • 解决 net core 3.x 跨域问题
    跨域:指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。以下几种情况是造成跨域的原因:域名相同,端口不......