1.问题描述
200(成功) 服务器已成功处理了请求,一般用于GET与POST请求
400 客户端请求的语法错误,服务器无法理解
500 服务器内部错误,无法完成请求
![](https://ywhao.bitworkshop.cn/wp-content/uploads/2022/03/image-51.png)
200成功但是前端无法收到,就是跨域问题,即红色错误提示
![](https://ywhao.bitworkshop.cn/wp-content/uploads/2022/03/image-49.png)
2.解决方法
安装django-cors-headers
pip install django-cors-headers
修改setting配置文件
添加'corsheaders' (这个好像并不影响)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testapp',
'corsheaders', # ajax跨域
]
![](https://ywhao.bitworkshop.cn/wp-content/uploads/2022/03/image-45.png)
设置CORS_ORIGIN_ALLOW_ALL为True
CORS_ORIGIN_ALLOW_ALL = True # ajax跨域
![](https://ywhao.bitworkshop.cn/wp-content/uploads/2022/03/image-46.png)
添加'corsheaders.middleware.CorsMiddleware',注意顺序
'corsheaders.middleware.CorsMiddleware', # ajax跨域,注意顺序
![](https://ywhao.bitworkshop.cn/wp-content/uploads/2022/03/image-47.png)
ajax跨域问题解决
![](https://ywhao.bitworkshop.cn/wp-content/uploads/2022/03/image-48.png)
Comments NOTHING