django寫的接口移動(dòng)端可以調(diào)用,用web訪問(wèn)報(bào)錯(cuò)如下:
image.png
建一個(gè)中間件py文件膝蜈,middlewares.py:
from django.utils.deprecation import MiddlewareMixin
class CrossDomainMiddleware(MiddlewareMixin):
def process_response(self, request, response):
#跨域中間件
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Headers'] = 'Content-Type'
return response
然后在settings.py的中間件MIDDLEWARE里進(jìn)行引用:
image.png
blog.middlewares.CrossDomainMiddleware
注意路徑误阻,前面是項(xiàng)目名抖所,中間是中間件py文件名,后面是middlewares.py里的方法名祖秒,我的middlewares.py文件路徑如下
image.png
再去請(qǐng)求已經(jīng)沒(méi)有毛病了:
image.png