3xx重定向,“请求的资源上不存在'Access-Control-Allow-Origin'标头”错误

时间:2019-09-26 11:58:26

标签: django vue.js cors axios django-cors-headers

Repository with the project

我正在通过CORS策略获取有关阻止访问的信息,而我看过的解决方案似乎都没有用。

error message

Access to XMLHttpRequest at 'http://localhost:8000/search' from origin 'http://localhost:8080' has been blocked by CORS policy: No 
'Access-Control-Allow-Origin' header is present on the requested resource.

我的服务器本地托管在localhost:8000上,前端位于loacalhost:8080上。我想在文件store.js%43:56中使用Axios在Vue中发布文本数据 :

   axios.post('http://localhost:8000/search', {
      queryText: 'TEST_TEXT'
   }).then(function (response) {
      // handle success
      console.log(response)
   }).catch(function (response) {
      // handle error
      console.log(response)
   })

后端是使用REST API用Django编写的。我遵循了those instructions,但响应仍然相同。负责响应的视图为here,并且设置为here

我不想:

  • 在Chrome中禁用网络安全性
  • 使用JSONP
  • 使用第三方网站重新路由我的请求

如果任何人想运行该项目,只需从主文件夹中安装requirements.txt并遵循主文件夹和前端文件夹中的自述文件即可。

1 个答案:

答案 0 :(得分:1)

您需要在Django中启用CORS设置,请查看django-cors-headers

此外,如果您要进行基于会话的身份验证,则可能必须在标头中添加一些额外的设置。不久前,我在Angular2上进行了挣扎,但我认为如果您遇到此问题,this answer对您来说就很重要。