简体   繁体   中英

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

$.ajax() request from html page in django project,it work with firefox but not working with chrome .i tried many things from google but not getting any result ,i waste my 2 day plz help Error on chrome console :

XMLHttpRequest cannot load http://example.com/myapp/jsonurl/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://www.example.com ' is therefore not allowed access.

My code :

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:

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

view:

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

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

Two solutions:

Make sure you use the same name (www.example.com in both for example)

Or if you have to keep two different name, make sure the server serving the AJAX call add a header: Access-Control-Allow-Origin: http://www.example.com>

More info about can be found on: http://jquery-howto.blogspot.com/2013/09/jquery-cross-domain-ajax-request.html

finally i got solution there are two problem in my project

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

2: i used "@login_required" with my view when remove this work properly with chrom all browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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