1

I keep getting the CORS error

Request header field Accept-Encoding is not allowed by Access-Control-Allow-Headers

This only happens when I am accessing my live/production server for ONE of my API's.

All the other API's can be accessed without any CORS error.

WORKING API (No CORS error):

    qset = (
        Dummyproduct.objects
        .order_by('-updated',)
        .values('storelogo', 'storeimage', 'productname', 'productimage', 'productprice',)
    )

    return Response(qset)

BROKEN API (CORS Error):

    qset = (
        {'description': 'Total Products' , 'stats':Product.objects.count(), 'icon':'face', 'color':'red','percent':89, },
        {'description': 'Total Swipes' , 'stats':Variation.objects.count(), 'icon':'person', 'color':'greed', 'percent':40,},
        {'description': 'Total Returns' , 'stats':User.objects.count(), 'icon':'refresh', 'color':'red', 'percent':62,},
        {'description': 'Total Purchases' , 'stats':User.objects.count(), 'icon':'money', 'color':'green', 'percent':32,}
    )

    return Response(qset)

Server (Django):

class CorsMiddleware(object):

def process_response(self, request, response):
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
    # response['Content-Type'] = 'application/json'
    response['Accept'] = 'application/json'
    return response

App (AngularJS):

function ($httpProvider) {
    $httpProvider.defaults.headers.common = {};
    $httpProvider.defaults.headers.post = {};
    $httpProvider.defaults.headers.put = {};
    $httpProvider.defaults.headers.patch = {};
    $httpProvider.defaults.headers.get = {};
}

The actual API has the following headers (Chrome>Network tools):

Accept:application/json
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT
Access-Control-Allow-Origin:*
Allow:GET, HEAD, OPTIONS
Content-Type:application/json;q=0.8
Date:Tue, 17 May 2016 04:02:04 GMT
Server:WSGIServer/0.1 Python/2.7.10
X-Frame-Options:SAMEORIGIN
Ycon
  • 1,830
  • 3
  • 27
  • 56

2 Answers2

0

Resolved this. Was very simply- I simple needed a trailing slash. Strange as using the page locally - I had no CORS error. Handy to know.

/mypage instead of /mypage/

Ycon
  • 1,830
  • 3
  • 27
  • 56
0

You need to set Access-Control-Request-Headers with a value of Accept-Encoding to fix this. Check this.

Community
  • 1
  • 1
Venkat
  • 1,095
  • 2
  • 13
  • 25