使用Django在服务器端写了一个API,返回一个JSON数据。使用Ajax调用该API:
但是,Chrome浏览器提示错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是由于CORS导致的。
安装django-cors-headers这里还有一各发现!在Django中,有人开发了CORS-header的middleware,只在settings.py中做一些简单的配置即可,见:https://github.com/ottoyiu/dj...,开启CORS,没有跨域烦恼,真爽!~
首先安装 pip install django-cors-headers
然后在settings.py里配置一番就可以 INSTALLED_APPS = [
...
'corsheaders',
... ]
MIDDLEWARE_CLASSES = (
...
#尽可能靠前,必须在CsrfViewMiddl