Skip to content

Add support for Cross Origin Resource Sharing #15

@vrutsky

Description

@vrutsky

On order to use jsonrpc library as server in crossdomain communications with JavaScript client in web-page, server should implement CORS [1].

Not sure how correctly implement it, but I managed to use it with following workaround outside jsonrpc library --- handle OPTIONS request that is send by browser to check whether CORS is allowed and add allow headers in all responses:

def add_CORS_headers(request):
    headers = request.responseHeaders
    headers.addRawHeader('Access-Control-Allow-Origin', '*')
    headers.addRawHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
    # This is necessary for Firefox
    headers.addRawHeader('Access-Control-Allow-Headers', 'Origin, Content-Type, Cache-Control')


class MyServer(JSON_RPC):
    def render_OPTIONS(self, request):
        add_CORS_headers(request)
        return ""

    def render(self, request):
        if request.method == "OPTIONS":
            return self.render_OPTIONS(request)
        else:
            add_CORS_headers(request)
            return JSON_RPC.render(self, request)

[1] https://secure.wikimedia.org/wikipedia/en/wiki/Cross-origin_resource_sharing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions