react-native(三)访问django服务器

叶阳
2023-12-01

django 运行服务器使用

python manage.py runserver 0.0.0.0:8000

settings中配置

ALLOWED_HOSTS = ["*"]

中间件注释掉跨域攻击

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

react-native fetch看官方文档即可
请求方式为这种服务端可以获取数据

fetch('https://mywebsite.com/endpoint/', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: 'key1=value1&key2=value2',
});
 类似资料: