亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django 和 React:錯誤消息“您需要啟用 JavaScript 才能運行此應用程序。”

Django 和 React:錯誤消息“您需要啟用 JavaScript 才能運行此應用程序。”

搖曳的薔薇 2023-06-09 15:04:09
我是 React 的新手,我嘗試讓 Django Rest 和 React 一起工作。順便說一句,我的 javascript 已啟用。我有一個簡單的看法:class StudentView(generics.ListCreateAPIView):    queryset = Student.objects.all()    serializer_class = StudentSerializer我嘗試從反應中獲取它:  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))      }, [])當我檢查瀏覽器網絡時,我得到的響應是:我不認為我有 CORS 標頭問題,但這是我的 CORS 設置。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',)我假設我做錯了什么,但我不知道。
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

好吧,我真的不知道為什么,但我讓它與 Axios 一起工作,而根本沒有改變我的 CORS 設置。如果有人知道為什么它使用 axios 而不是使用 fetch,我想知道。


因此,如果這里發生在其他人身上,則工作代碼會使用 axios 作出反應:


  axios.get("http://127.0.0.1:8000/secretariat/get_student/")

    .then(response => {

      console.log(response)

      })

    .catch(error => {

      console.log(error);

    })


查看完整回答
反對 回復 2023-06-09
  • 1 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號