繁体   English   中英

XMLHttpRequest无法加载http://example.com/myapp/jsonurl/。 请求的资源上不存在“ Access-Control-Allow-Origin”标头

[英]XMLHttpRequest cannot load http://example.com/myapp/jsonurl/. No 'Access-Control-Allow-Origin' header is present on the requested resource

$.ajax()来自django项目中html页面的请求,它与firefox一起工作,但与chrome不一起工作。

XMLHttpRequest无法加载http://example.com/myapp/jsonurl/. 所请求的资源上没有'Access-Control-Allow-Origin'标头。 因此,不允许访问来源“ http://www.example.com ”。

我的代码:

ajax code:
 $.ajax({
    url: "http://example.com/myapp/jsonurl/",
    dataType:"json",
    type:"GET",
    success: function(data){
      alert(data);
    },
    error: function(xhRequest, ErrorText, thrownError){
      alert(xhRequest+ ErrorText+thrownError);
    }
  });

网址:

url(r'^jsonurl/', planViews.jsonurl,name='jsonurl'),

视图:

def jsonurltest(request):
    context = RequestContext(request)

    return HttpResponse(json.dumps("{'jsondata':'ajax test'}"), content_type="application/json")

两种解决方案:

确保使用相同的名称(例如,两者都使用www.example.com)

或者,如果必须保留两个不同的名称,请确保为AJAX调用提供服务的服务器添加标头:Access-Control-Allow-Origin:http://www.example.com>

有关更多信息,请访问: http//jquery-howto.blogspot.com/2013/09/jquery-cross-domain-ajax-request.html

终于我得到了解决方案我的项目中有两个问题

1:安装https://github.com/ottoyiu/django-cors-headers

2:在使用chrom all浏览器正确删除此工作时,我在视图中使用了“ @login_required”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM