1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# How would I add `Access-Control-Allow-Origin: *` header to this view?

@view_config(route_name="stats-json", renderer="json")
def stats_json(request):
    return request.namespace.stats



# I ended up bypassing the json renderer and setup my own response object.
# Is there a better way?

@view_config(route_name="stats-json")
def stats_json(request):
    from json import dumps
    response = Response(body=dumps(request.namespace.stats))
    response.headerlist = []
    response.headerlist.extend(
        (
            ('Access-Control-Allow-Origin', '*'),
            ('Content-Type', 'application/json')
        )
    )
    return response