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

Web Plugin Access-Control-Allow-Origin header not present #1236

Closed
andremiller opened this issue Jan 17, 2015 · 7 comments
Closed

Web Plugin Access-Control-Allow-Origin header not present #1236

andremiller opened this issue Jan 17, 2015 · 7 comments
Labels
feature features we would like to implement

Comments

@andremiller
Copy link
Contributor

I'm putting together a client-side HTML5/Javascript player that uses the Web plugin's REST/JSON API.

Because the source files for the client is not hosted by the flask web server, I'm getting the following error in the browser when I try to make API calls using XMLHttpRequest in Javascript:

XMLHttpRequest cannot load http://beets:8337/item/716. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://musicplayer:9000' is therefore not allowed access.

Is there a way the beets web server can be configured to provide this header so remote browsers can access the API?

PS: I found this link which has an example of how to implement it in Flask: http://flask.pocoo.org/snippets/56/

@andremiller
Copy link
Contributor Author

Just tested the snippet I refer to above and it works. I placed the whole snippet into a new file called crossdomain.py inside the beetsplug/web directory and then edited beetsplug/web/init.py as such:

Near the top:
from crossdomain import crossdomain

Below each route or pair of routes, added:
@crossdomain(origin='*')

For example:

@app.route('/item/<idlist:ids>')
@crossdomain(origin='*')
@resource('items')
def get_item(id):
    return g.lib.get_item(id)

@app.route('/item/')
@app.route('/item/query/')
@crossdomain(origin='*')
@resource_list('items')
def all_items():
    return g.lib.items()

@sampsyo sampsyo added the feature features we would like to implement label Jan 17, 2015
@sampsyo
Copy link
Member

sampsyo commented Jan 17, 2015

Hi! Awesome; I'm glad you're interested in working on this.

I'm not sure if it's clear from the paper-trail of recent issues, but there is a new effort to start from scratch with the web API. See #738, #718, #1002, and AURA. The AURA reference server has CORS enabled.

So we can add a cross-domain header to the current plugin in the short term (care to open a PR?), but soon we will be throwing this one away and adding a new backend. So if you're building a new frontend, let's stay in touch—it would be great to get compatible with the new backed as quickly as possible.

@andremiller
Copy link
Contributor Author

Thanks sampsyo. I'll go check out AURA, I didn't know it existed!

I discovered beets about two days ago after getting frustrated with online streaming services and decided to turn my own music collection into something I can stream. It looked perfect but I wanted a better web player, so of course I jumped in and started hacking without looking at all the documentation and current development :).

@sampsyo
Copy link
Member

sampsyo commented Jan 17, 2015

Awesome; I think we have exactly the same goal:

I discovered beets about two days ago after getting frustrated with online streaming services and decided to turn my own music collection into something I can stream.

😃

My ultimate dream with AURA is indeed to have a good, interoperable alternative to streaming services.

@andremiller
Copy link
Contributor Author

In the meantime I created a pull request. My first one, so I am not sure I did it 100% correct.

@andremiller
Copy link
Contributor Author

@sampsyo I hacked together a quick example here: https://github.com/andremiller/beets-player-samples/tree/master/w2ui

This still uses the beetsweb plugin and it's quite ugly, more of a proof of concept.

@sampsyo
Copy link
Member

sampsyo commented Jan 18, 2015

Very cool! This looks useful already. I'll totally give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature features we would like to implement
Projects
None yet
Development

No branches or pull requests

2 participants