Question: Is it possible automatically add `Access-Control-Allow-Origin` header to all responses which was initiated by ajax request (with header `X-Requested-With`) in Pyramid?

Answer: 
There are several ways to do this: 1) a custom request factory like drnextgis showed, a NewRequest event handler, or a tween. A tween is almost certainly not the right way to do this, so I won't show that. Here is the event handler version:

```
def add_cors_headers_response_callback(event):
    def cors_headers(request, response):
        response.headers.update({
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'POST,GET,DELETE,PUT,OPTIONS',
        'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization',
        'Access-Control-Allow-Credentials': 'true',
        'Access-Control-Max-Age': '1728000',
        })
    event.request.add_response_callback(cors_headers)

from pyramid.events import NewRequest
config.add_subscriber(add_cors_headers_response_callback, NewRequest)

```
I could send file with Ajax from a server to another server :

```
import uuid

from pyramid.view import view_config

from pyramid.response import Response

class FManager:

    def __init__(self, request):
        self.request = request

    @view_config(route_name='f_manager', request_method='POST', renderer='json')
    def post(self):
        file_ = self.request.POST.items()
        content_type = str(file_[0][1].type).split('/')
        file_[0][1].filename = str(uuid.uuid4()) + '.' + content_type[1]
        file_id = self.request.storage.save(file_[0][1])

        response = Response(body="{'data':'success'}")
        response.headers.update({
            'Access-Control-Allow-Origin': '*',
        })

        return response

```