SoFunction
Updated on 2024-11-10

The solution to the cross-domain problem of the flask framework in Python

I. What is cross domain

The process of requesting from one domain to another is called cross-domain. Browsers go from a web page in one domain to request a resource in another domain, and one of the domain names, ports, and protocols are not the same, the request is cross-domain. Cross-domain is actually a protection policy of the browser.

It's on the web page.ajaxupon request,will report:No ‘Access-Control-Allow-Origin' header is present on the requested 'This error.。

Second, how to solve the cross-domain problem

1. The process of cross-domain requests

请添加图片描述

So we just need to make the request header information consistent.
1, in the program code to add, my back-end with python's flask framework, so add this paragraph in the app object can be

from flask_cors import *
    #Add header fields allowed for cross-domain requests
    # Add the access-control-origin header field to this current flask core object app
    # All urls under / are allowed to be accessed by all origins
    CORS(app, resources={r"/*": {"origins": "*"}})

2, in the nginx server to modify the configuration of nginx, configuration file as follows:

在这里插入图片描述

summarize

This article on the Python flask framework cross-domain problem solving method is introduced to this article, more related flask cross-domain content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!