“否”访问控制允许起始标头存在于所请求的资源“在django [英] "No 'Access-Control-Allow-Origin' header is present on the requested resource" in django

查看:267
本文介绍了“否”访问控制允许起始标头存在于所请求的资源“在django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的新手,并将其用作创建用户的应用程序的后端。在前端,发布用户名的代码是:

I am newbie to django and using it as back end for an application that creates users. In front end the code for posting the user name is :

var xobj = new XMLHttpRequest();
              xobj.overrideMimeType("application/json");
              xobj.open('POST', "http://www.local:8000/create_user/", true);
                xobj.setRequestHeader("Access-Control-Allow-Origin", "*");
              xobj.onreadystatechange = function () {
                  if (xobj.readyState == 4 && xobj.status == "200") {
                      console.log(xobj.responseText);
                  }
            }
              xobj.send(json);    

在后端,与url关联的函数处理json,但是我收到错误
请求的资源上不存在访问控制允许源标题。 http://www.local:54521 因此不允许访问。
这个问题的解决方案是什么?
此外,我还按照 https://gist.github.com/strogonoff/1369619 中的步骤操作,但问题仍然存在。

On back end the function associated with url handles json but i am getting the error "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.local:54521' is therefore not allowed access". What is the solution for this problem? Also I have followed the steps from "https://gist.github.com/strogonoff/1369619", but problem persists.

推荐答案

您的前端和后端位于不同的端口,这意味着您的ajax请求需要跨源安全

Your front and back end are on different ports which means your ajax requests are subject to cross origin security.

您需要设置后端以接受来自不同来源(或仅不同端口号)的请求。

You need to set up the back end to accept requests from different origins (or just different port numbers).

尝试阅读 CORS ,更具体地看看 django cors标题

Try reading up on CORS and more specifically looking at django cors headers

这篇关于“否”访问控制允许起始标头存在于所请求的资源“在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆