Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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'

)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report