我正在开发一个全栈的django和react应用程序,目前它只是从API中获取数据,我也可以获取第一个项目的详细信息页面,但不能为第二个项目做同样的事情。它显示以下错误,当我试图从django获取第二个项目(奇怪,但它对id = 1的第一项工作得很好,而不是对id = 2或3)。
另外,我把我的整个代码都放到了bitbucket上。您可以在这里调试前端和后端https://bitbucket.org/Yash-Marmat/react-django-blog/src/master/
先谢谢你了。
3条答案
按热度按时间ruarlubt1#
你需要使用
proxy
密钥在内部前端package.json
文件中,如下所示并重新启动React应用程序。
gv8xihay2#
您的URL看起来很奇怪。请尝试更改URL:
并在前端使用更改的URL访问。我可以推荐一个good tutorial for Django+React CRUD。
8yparm6h3#
您需要在您的django API中启用cors或跨源资源共享。
你可以通过安装这个叫做django-cors-headers的软件包来实现
Django cors headers github page
然后将其添加到已安装的应用程序:
您还需要添加一个中间件类来侦听响应:
更多详情:https://github.com/ottoyiu/django-cors-headers/#configuration
阅读官方文档可以解决几乎所有问题