Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Using CORS to solve vue.js django Cross-domain call

Shulou Source: shulou.com Published: 2022-06-03 06:42:54 09月25日 Update

Cross-Origin Resource Sharing (CORS) cross-domain resource sharing is a specification of browser technology, which provides a way for Web services to send sandboxie scripts from different domains to avoid the homologous strategy of browsers. It is a modern version of JSONP mode. Unlike JSONP, CORS supports other HTTP requirements in addition to the GET requirement method. Using CORS allows web designers to use normal XMLHttpRequest, which is better than JSONP for error handling. On the other hand, JSONP can work on older browsers that don't support CORS. Modern browsers support CORS.

Introduction of django-cors-middleware

Pip install django-cors-middleware

Add it in settings.py. Note: if you don't add it, it will not take effect.

INSTALLED_APPS = (

...

'corsheaders'

)

Add middleware snooping

MIDDLEWARE = [

...

'corsheaders.middleware.CorsMiddleware'

'django.middleware.common.CommonMiddleware'

...

]

Configure domain names that allow cross-domain access

CORS_ORIGIN_ALLOW_ALL = False

CORS_ORIGIN_WHITELIST = (

'google.com'

'hostname.example.com'

)

The default value is all:

CORS_ORIGIN_WHITELIST = ()

Or define the regular expression of the allowed matching path.

CORS_ORIGIN_REGEX_WHITELIST = ('^ (https?://)? (\ wicked.) > google.com$',)

Default value:

CORS_ORIGIN_REGEX_WHITELIST = ()

Set the method to allow access:

CORS_ALLOW_METHODS = (

'GET'

'POST'

'PUT'

'PATCH'

'DELETE'

'OPTIONS'

)

Set the allowed header:

Default value:

CORS_ALLOW_HEADERS = (

'xmusic, requestedhouse, with.'

'content-type'

'accept'

'origin'

'authorization'

'xMurcsrftoken'

)

Tags: Browsing methods support different middleware on the other hand domain names technologies methods patterns rules policies web pages scripts expressions designers resources resource sharing paths Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Docker Microsoft Linux