Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Cross Origin Resource Sharing #15

Open
vrutsky opened this issue Apr 27, 2012 · 0 comments
Open

Add support for Cross Origin Resource Sharing #15

vrutsky opened this issue Apr 27, 2012 · 0 comments

Comments

@vrutsky
Copy link

vrutsky commented Apr 27, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant