gpt4 book ai didi

reactjs - 'http ://localhost:300 0' has been blocked by CORS policy: No ' Access-Control-Allow-Origin' header is present on the requested resource

转载 作者:行者123 更新时间:2023-12-04 12:36:00 27 4
gpt4 key购买 nike

我正在使用 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)
})
}

getImageClass =(obj)=> {
axios.get(`http://127.0.0.1:8000/api/encrypt/${obj.data.id}/`, {
headers: {
'accept': 'application/json',
}
})
.then(resp=>{
this.setState({recentImage:resp})
console.log(resp)
})
.catch(err=>{
console.log("Code broke at get image")
console.log(err)
})
this.deactivateSpinner()

}

最佳答案

这绝对是后端的问题,我的意思是 Django。CORS_ALLOW_ALL_ORIGINS=True一旦你设置了 CORS_ALLOW_ALL_ORIGINS值,您还需要设置 ALLOWED_HOSTS 的值.
例如ALLOWED_HOSTS=['*']请查看以下链接。https://pypi.org/project/django-cors-headers/https://dzone.com/articles/how-to-fix-django-cors-error

关于reactjs - 'http ://localhost:300 0' has been blocked by CORS policy: No ' Access-Control-Allow-Origin' header is present on the requested resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64744518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com