cross domain error
I have install your nice api to sonos on a raspberry, it is working well calling it from a browser in my network with 192.168.0.32:5005/xxx/play. when I try to call from another web frontend, also running on the rasp, using a small javascript xmlhttprequest get http://192.168.0.32:5005/xxx/play, I get into cross domain trouble: Origin http://192.168.0.32:3000 is not allowed by Access-Control-Allow-Origin. So I'm trying to find the place in your code to put in the allow-... I'm very new to rasp, web, html,.. so maybe this is all wrong, but it seems so close to be solved
I think you need to capture the actual headers that are sent in order to identify what might be the cause of this block. There are some restrictions on what content types and what headers that are allowed to be passed on the request that might be the issue here.
Hi
I have install wireshark on my imac to check what information is sent, to and from the rasp. And actually I get a ok 200 response, but my browse safari/chrome/firefox will not allow it. everything I have found on the internet on this allow origin error, states that it takes a few option on the response. I know I'm asking a lot here, but I can't find the place in your code to test something like this http://enable-cors.org/server_expressjs.html, so could you guide to the plate in your code, why the response is build, and where I can try to change/add the options ?
Well, that's how cross domain restrictions is applied. If you could show me the headers that are sent back and forth, I might be able to identify what it is that is restricting it.
Sorry to barge in. Johnny, the response is build in GETrouting in sonos-http-api.js. if you want to add the CORS headers for every request (even those that return an empty response like "Play" in your example url) you can add
res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
just above res.end();
thanx for barging in, steffest, it works :-) I had tried to put the first line in that JS file, but a few lines to early, but obviously I needed the other line. Now I just need to find a similar place in the other node.js remote control thing from alexba.in
thanx a lot for fast answering.
and once again thanx to jishi for all the wonderful code.
@steffest thanks for looking into this.
I assume it's the X-Requested-With header added by jQuery that gave trouble (the other headers should perhaps be fine).
We could maybe add something to allow any custom header, something like this:
res.setHeader("Access-Control-Allow-Headers", req.getHeader("Access-Control-Request-Headers"));
Anybody have reproduction steps for this? I think one might need to implement correct handling of OPTIONS requests as well, to fully support this.
Failed to load resource: the server responded with a status of 404 (Not Found) P1:1 GET http://192.168.1.3:5005/Toalett/P1 404 (Not Found)
Have I missed something? works fine from the gui but I can't seem to send requests...
tack
@clafa
You seem to be missing the actual action that you want to invoke. I assume "P1" is a favorite, so it should be:
http://192.168.1.3:5005/Toalett/favorite/P1
GET http://192.168.1.3:5005/Toalett/favorite/P1 404 (Not Found) Navigated to http://192.168.1.3:5005/Toalett/favorite/P1
sorry... wrong in my copy paste.... This seems correct does'nt it?
@clafa the HTTP API should not return 404 on neither missing room or missing action (or missing favorite), so there must be something else that triggers this. And please open a new ticket for this since it is unrelated to the original question. We notify all participants every time we reply in this issue.
I ran into an issue with this recently as well.
The "fix" in this thread doesn't work in all scenarios because a lot of don't have a response going through the callback.
@steffest you suggested adding the following right above res.end()
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
Did you mean inside the if (response) { or not?
https://github.com/jishi/node-sonos-http-api/blob/master/lib/sonos-http-api.js#L63
Also, lets just use the 'group' function as an example. None of them send any data when they hit that callback, so the code doesn't run through the if (response) block. When I tried adding an 'else' condition to that block, nothing worked. So, I stopped for a while.
@sorahn Have you found any solution to this?
I have the same problem.
This is actually fixed in the beta, it correctly responds to OPTIONS requests and will respond with any extraneous headers that is explicitly asked for to tell the pre-flight that the request is OK.