小编Tim*_*ter的帖子

“http://localhost:3000”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头

我正在使用 Django 和 React 使用 Rest Framework 进行项目。我已经CORS_ALLOW_ALL_ORIGINS=True在 settings.py 中设置了,但仍然出现错误Access to XMLHttpRequest at 'http://127.0.0.1:8000/api/encrypt/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我正在使用 axios 来发布和获取请求。令人惊讶的是,即使发出错误后请求但获取请求失败。这是使用 axios 的 React 文件

sendImage =()=> {
     this.activateSpinner()
     let formData = new FormData()
     formData.append('to_be_hidden', this.state.files[0], this.state.files[0].name)
     formData.append('used_to_hide', this.state.files[1], this.state.files[1].name)
     axios.post('http://127.0.0.1:8000/api/encrypt/', formData, {
         headers: {
            'accept': 'application/json',
            'content-type': 'multipart/form-data'
         }
     })
     .then(resp=>{
         this.getImageClass(resp)
         console.log(resp.data.id)
     })
     .catch(err=>{
         console.log("Code broke at send image")
         console.log(err)
     }) …
Run Code Online (Sandbox Code Playgroud)

django cross-domain django-rest-framework reactjs django-cors-headers

6
推荐指数
2
解决办法
2万
查看次数