Getting a “No 'Access-Control-Allow-Origin'

2019-07-19 01:15发布

问题:

Further to the previous question - Uber API - HTTPS needed for Price Estimates? - I have set up https on our site.

My App on Uber Developer is set up with the redirect URL & Origin URL being the https domain.

Following the code on this page - https://developer.uber.com/v1/api-reference/#request-response - I've tried testing the code given here:-

var xhr = new XMLHttpRequest();
xhr.setRequestHeader("Authorization", "Token YOUR_SERVER_TOKEN");
xhr.open('GET', 'https://api.uber.com/v1/products?latitude=37.7759792&longitude=-122.41823');

Unfortuantely I'm getting the error "Uncaught InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.".

I've then switched around the .setRequestHeader & .open lines and I am getting the following error:-

"XMLHttpRequest cannot load https://api.uber.com/v1/estimates/price?start_latitude=53.4521477&start_longitude=-2.2750512&end_latitude=53.4522973&end_longitude=-2.275001. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://milliondollarwiki.co.uk' is therefore not allowed access."

Here is the final code in question:-

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.uber.com/v1/estimates/price?start_latitude=' + position.coords.latitude + '&start_longitude=' + position.coords.longitude + '&end_latitude=53.4522973&end_longitude=-2.275001');
xhr.setRequestHeader("Authorization", "Token bTVn8k_wF3JpmHlZ0GhdbMWaLgbLMSaYof8rs-S_");        
xhr.send();

The URL which is interacting in the Uber API is in a subfolder of the Redirect & Origin URLs, but I don't think that should cause an issue.

Any idea on what is going wrong?

回答1:

I looked up your application and at this time you have

https://milliondollarwiki.co.uk/scratchpad/

as the ORIGIN URI. For CORS, you should only be specifying the server address without a specified path (and no trailing slash). Try changing it to:

https://milliondollarwiki.co.uk

and see if that fixes your problem.



回答2:

Right I managed to get it fixed :). I had to recreate the app which took a minute or two but it was fine. Odd :-/



标签: uber-api