You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					26 lines
				
				516 B
			
		
		
			
		
	
	
					26 lines
				
				516 B
			| 
											2 years ago
										 | from fastapi import FastAPI | ||
| 
											2 years ago
										 | from fastapi.middleware.cors import CORSMiddleware | ||
|  | 
 | ||
|  | 
 | ||
| 
											2 years ago
										 | def add_cors_middleware(app: FastAPI): | ||
| 
											1 year ago
										 |     """
 | ||
|  |     添加跨域中间件 | ||
|  | 
 | ||
|  |     :param app: FastAPI对象 | ||
|  |     :return: | ||
|  |     """
 | ||
| 
											2 years ago
										 |     # 前端页面url | ||
|  |     origins = [ | ||
| 
											1 year ago
										 |         'http://localhost:80', | ||
|  |         'http://127.0.0.1:80', | ||
| 
											2 years ago
										 |     ] | ||
| 
											2 years ago
										 | 
 | ||
| 
											2 years ago
										 |     # 后台api允许跨域 | ||
|  |     app.add_middleware( | ||
|  |         CORSMiddleware, | ||
|  |         allow_origins=origins, | ||
|  |         allow_credentials=True, | ||
| 
											1 year ago
										 |         allow_methods=['*'], | ||
|  |         allow_headers=['*'], | ||
| 
											2 years ago
										 |     ) |