Skip to content
This repository has been archived by the owner on Dec 20, 2020. It is now read-only.

OPTIONS issue #99

Closed
solisoft opened this issue Jan 11, 2016 · 19 comments
Closed

OPTIONS issue #99

solisoft opened this issue Jan 11, 2016 · 19 comments

Comments

@solisoft
Copy link

qwest.get(my_IP')
     .then(function(xhr, response) {
        alert(response);
     });

Raise an error Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

How can I add "Access-Control-Allow-Origin: *" in headers ?

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

You have an headers option available (see the docs). But I think you're doing a CORS request, then you must have the according headers set on the response from your server : http://www.html5rocks.com/en/tutorials/cors/

@pyrsmk pyrsmk closed this as completed Jan 11, 2016
@solisoft
Copy link
Author

Well the same request using jQuery $.get method works fine !?

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

They probably have some additional code. Did you test the headers option or anything else from the article?

@solisoft
Copy link
Author

Yes they have Access-Control-Allow-Origin: * in the request header ... I wasn't able to add it with qwest

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

The Access-Control-Allow-Origin: * needs to come from the server, not from qwest. As the returned error is saying, especially in the last sentence : No 'Access-Control-Allow-Origin' header is present on the requested resource.

That's why I've posted this article about CORS : http://www.html5rocks.com/en/tutorials/cors/

Because it seems you're not understanding well what is happening ^^

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

Is the example you've posted above is the complete code for the request? You have no data or other options set on qwest?

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

To well understand what's happening on the request, read this specific section : http://www.html5rocks.com/en/tutorials/cors/#toc-adding-cors-support-to-the-server

Furthermore, CORS is activated by your request because it seems you're running the request on a different domain than the resource itself.

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

With PHP a simple header('Access-Control-Allow-Origin: *'); do the trick ;)

@solisoft
Copy link
Author

On my ruby script I use :

before do
    response['Access-Control-Allow-Origin'] = "*" # Allow any origin
 end

Without that, jQuery raise an error too !

But here the response after a simple $.get

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 136
Connection: keep-alive
Status: 200 OK
Access-Control-Allow-Origin: *
Date: Mon, 11 Jan 2016 16:19:22 GMT
X-Powered-By: Phusion Passenger 5.0.23
Server: nginx/1.8.0 + Phusion Passenger 5.0.23

As you can see, Access-Control-Allow-Origin: * is there.

I will copy /paste both jquery and Qwest headers and both answers

And yes I use XDomain :)

@solisoft
Copy link
Author

I Just found my issue ... Qwest send an OPTIONS ...then my webservice raise a 404 error
It is normal ?

jQuery (OK)

$.get("http://myip/vehicles/search")

Request

GET /vehicles/search HTTP/1.1
Host: 167.114.253.203
Connection: keep-alive
Accept: /
Origin: http://localhost:8081
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Referer: http://localhost:8081/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4

Response

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
Connection: keep-alive
Status: 200 OK
Access-Control-Allow-Origin: *
Date: Mon, 11 Jan 2016 16:36:12 GMT
X-Powered-By: Phusion Passenger 5.0.23
Server: nginx/1.8.0 + Phusion Passenger 5.0.23

Qwest (NOK)

qwest.get("http://myip/vehicles/search")

Request

OPTIONS /vehicles/search HTTP/1.1
Host: 167.114.253.203
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:8081
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Access-Control-Request-Headers: accept, cache-control
Accept: /
Referer: http://localhost:8081/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4

Response

HTTP/1.1 404 Not Found
Content-Length: 9
Connection: keep-alive
Status: 404 Not Found
X-Cascade: pass
Date: Mon, 11 Jan 2016 16:36:08 GMT
X-Powered-By: Phusion Passenger 5.0.23
Server: nginx/1.8.0 + Phusion Passenger 5.0.23

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

Yes it's normal. The explanations from a previous issue : #90
Maybe it could fix your problem.

@solisoft
Copy link
Author

Yep, adding { cache:true } works for me too ! Thank you for your help !

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

Great!

This OPTIONS issue is really annoying by the way...

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

I'm reopening this issue to have a reminder. I need to modify how the qwest's cache is working.

@pyrsmk pyrsmk reopened this Jan 11, 2016
@solisoft
Copy link
Author

nice, good luck :)

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

Thanks ;)

@solisoft solisoft changed the title Access-Control-Allow-Origin is missing in headers OPTIONS issue Jan 11, 2016
@pyrsmk
Copy link
Owner

pyrsmk commented Jan 11, 2016

Related to this PR : #73

@pyrsmk
Copy link
Owner

pyrsmk commented Jan 12, 2016

I'll just update the doc since Cache-Control seems the best way to handle request caching.

@pyrsmk pyrsmk closed this as completed Jan 12, 2016
@cdll
Copy link

cdll commented Sep 19, 2016

oh thanks all above, i ve got this problem too and finally i found that i had wrongly spelling header as headers, wish anyone seeing this to check it out~ : )

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

No branches or pull requests

3 participants