我试图在localhost:8000上向Django REST Framework发送一个POST请求。当Postman发送请求时,我收到Django响应,但是我使用React.js应用程序使用fetch()函数接收控制台错误。我看到,对于React.js应用程序的请求,会向Django后端发送一个选项请求。
这是我在React应用程序上收到的错误。
Access to fetch at 'http://localhost:8000/api/admin/login/' from origin 'http://ucla.localhost:3000' has be
我使用reactJS作为前端,而Django作为后端。当我从react向Django发出请求时(react在http://127.0.0.1:3000上运行,而Django在http://127.0.0.1:8000上运行), Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/api/categories. (Reason: CORS header 'Access-Control-Allow-Origin
我有一个带有react前端和django后端的web应用程序。部分后端API被cors策略封堵,部分未被封堵。例如,用于获取博客帖子const res = await axios.get(`${process.env.REACT_APP_API_URL}/blog/posts`)的API正在返回数据,但是用于单个帖子const res = await axios.get(`${process.env.REACT_APP_API_URL}/blog/${slug}`);的API被阻塞。注册和登录API也被阻止。我已经将域名添加到settings.py中允许的url列表中,但这并没有解决问题。我
上下文:我使用的是django (1.10) + django-restframework。我正在实施一个应用程序,将需要从服务器实时更新。
我希望,从前端,能够接收正在数据库上执行的实时更新。我知道解决方案是使用websockets。然而,我希望避免不得不管理低级的websocket业务。我想知道是否有一个高级解决方案可以与django和django-restframework很好地集成在一起,如果没有,那将需要做很多工作,我应该从哪里开始。
作为一个用户,我想在后台做的事情是:
# define a model
class MyModel(models.Model):
myField
我已经在堆栈溢出和互联网上搜索了一个解决方案,可以从anotehr django restframework APIView调用django restframework ListAPIView。
我试过了:
class ViewA(APIView):
def get(request):
response = ViewB.as_view({'get':'list'})(request)
print response.render()
# do other things with the response
然而,
我已经尝试了react和django api之间所有可能的CORS组合。它仍然不起作用。
我得到了这个错误
has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.au