Skip to content

Instantly share code, notes, and snippets.

@aschem
Created October 24, 2011 12:06
Show Gist options
  • Save aschem/1308865 to your computer and use it in GitHub Desktop.
Save aschem/1308865 to your computer and use it in GitHub Desktop.
Django cross-domain Ajax view decorator
from django.http import HttpResponse
ACC_HEADERS = {'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Max-Age': 1000,
'Access-Control-Allow-Headers': '*'}
def cross_domain_ajax(func):
""" Sets Access Control request headers."""
def wrap(request, *args, **kwargs):
# Firefox sends 'OPTIONS' request for cross-domain javascript call.
if request.method != "OPTIONS":
response = func(request, *args, **kwargs)
else:
response = HttpResponse()
for k, v in ACC_HEADERS.iteritems():
response[k] = v
return response
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment