我是 React 的新手,我嘗試讓 Django Rest 和 React 一起工作。順便說(shuō)一句,我的 javascript 已啟用。我有一個(gè)簡(jiǎn)單的看法:class StudentView(generics.ListCreateAPIView): queryset = Student.objects.all() serializer_class = StudentSerializer我嘗試從反應(yīng)中獲取它: useEffect(() =>{ fetch("http://127.0.0.1:8000/secretariat/get_student/", { method: 'GET', headers: { 'Content-Type': 'application/json', } }) .then( resp => resp.json()) .then( resp => setStudents(resp)) .catch(error => console.log(error)) }, [])當(dāng)我檢查瀏覽器網(wǎng)絡(luò)時(shí),我得到的響應(yīng)是:我不認(rèn)為我有 CORS 標(biāo)頭問(wèn)題,但這是我的 CORS 設(shè)置。INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'corsheaders', 'common_app', 'rest_framework', 'rest_framework.authtoken', 'allauth.account', 'rest_auth.registration', 'ldap', 'rest_auth', 'simple_history',]MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'simple_history.middleware.HistoryRequestMiddleware',]CORS_ORIGIN_ALLOW_ALL = FalseCORS_ORIGIN_WHITELIST = ( 'http://127.0.0.1')CORS_ALLOW_METHODS = ( 'DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT',)CORS_ALLOW_HEADERS = ( 'accept', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with',)我假設(shè)我做錯(cuò)了什么,但我不知道。
Django 和 React:錯(cuò)誤消息“您需要啟用 JavaScript 才能運(yùn)行此應(yīng)用程序。”
搖曳的薔薇
2023-06-09 15:04:09