Django-cors-headers not working

拜拜、爱过 提交于 2019-12-01 19:45:54
Jai

Better to create a proxy at your application which in turn will call the other domain and will return you the data:

function getLeague() {
  $.ajax({
    url: '/crossdomainData',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
        alert('Success');
    },
    error: function(data) {
        alert('Fail');
    }
    });
}

As you are using django, you can import this Django HTTP Proxy.

Introduction

Django HTTP Proxy provides simple HTTP proxy functionality for the Django web development framework. It allows you make requests to an external server by requesting them from the main server running your Django application. In addition, it allows you to record the responses to those requests and play them back at any time.


Another option is here taken from this post answered by @dvcrn.

import urllib2
    def crossdomainData(request):
        url = "http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx"
        req = urllib2.Request(url)
        response = urllib2.urlopen(req)
        return HttpResponse(response.read(), content_type="application/json")

Some 500 errors happen earlier than the CORS middleware, so it has no chance to add CORS headers. If the response status code was 500, this might be the issue and CORS might be working fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!