【发布时间】:2017-10-17 15:52:05
【问题描述】:
我创建了一个带有 django-rest-framework 的 restful api,可以通过这个 URL http://192.168.33.10:8002/scenarios/ 访问,我正在创建一个 React 应用程序来调用 api 并使用它的数据。
我正在使用 fetch 调用 api
componentWillMount: function(){
this.setState({Problemstyle: this.props.Problemstyle})
fetch('http://192.168.33.10:8002/scenarios/')
.then(result=>result.json())
.then(result=> {
this.steState({items:result})
})
},
当我运行我的应用程序时,我的浏览器出现错误
Fetch API 无法加载 http://192.168.33.10:8002/scenarios/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'http://192.168.33.10:8001'。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
我刚刚开始使用 React,不知道如何解决这个问题
【问题讨论】:
-
您在192.168.33.10:8002/scenarios 的 django-rest-framework 应用程序需要配置为发送 Access-Control-Allow-Origin 响应标头。因为它当前没有返回该响应标头,所以您的浏览器不允许您的前端 JavaScript 代码访问响应。所以你可能想在192.168.33.10:8002/scenarios 服务器上安装github.com/ottoyiu/django-cors-headers
标签: django rest reactjs cors django-rest-framework