Created
June 18, 2015 13:26
-
-
Save mikebski/9adf20dae3355c87c071 to your computer and use it in GitHub Desktop.
Django 1.8 add Access-Control-Allow-Origin headers to response before sending
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from django.http import HttpResponse | |
from django.shortcuts import render | |
def _acao_response(response): | |
response['Access-Control-Allow-Origin'] = '*' | |
response['Access-Control-Allow-Methods'] = 'GET' | |
def home_view(request): | |
r = { 'response': 'OK', 'data': None } | |
response = HttpResponse(json.dumps(r), content_type = "application/json") | |
_acao_response(response) | |
return response | |
def ping(request): | |
r = { 'response': 'OK', 'data': 'ping' } | |
response = HttpResponse("['ok']", content_type="application/json") | |
_acao_response(response) | |
return response | |
def echo(request): | |
if request.method == 'GET': | |
data = request.GET | |
elif request.method == 'POST': | |
data = request.POST | |
else: | |
data = 'Unknown data for method' %(request.method) | |
r = { 'response': 'OK', 'method': request.method, 'data': request.GET } | |
response = HttpResponse(json.dumps(r), content_type = "application/json") | |
_acao_response(response) | |
return response | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment