【问题标题】:Getting a "No 'Access-Control-Allow-Origin' header is present on the requested resource." Error获取“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”错误
【发布时间】:2015-05-30 23:07:46
【问题描述】:

关于上一个问题 - Uber API - HTTPS needed for Price Estimates? - 我已经在我们的网站上设置了 https。

我在 Uber Developer 上的应用设置了重定向 URL 和源 URL 作为 https 域。

按照此页面上的代码 - https://developer.uber.com/v1/api-reference/#request-response - 我已尝试测试此处给出的代码:-

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');

不幸的是,我收到错误“Uncaught InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.”。

然后我切换了 .setRequestHeader 和 .open 行,我收到以下错误:-

“XMLHttpRequest 无法加载 https://api.uber.com/v1/estimates/price?start_latitude=53.4521477&start_longitude=-2.2750512&end_latitude=53.4522973&end_longitude=-2.275001。请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此不允许访问源 'https://milliondollarwiki.co.uk'。”

这是有问题的最终代码:-

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();

在 Uber API 中交互的 URL 位于 Redirect & Origin URLs 的子文件夹中,但我认为这不会导致问题。

知道出了什么问题吗?

【问题讨论】:

    标签: uber-api


    【解决方案1】:

    我查看了你的申请,此时你有

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

    作为 ORIGIN URI。对于 CORS,您应该只指定没有指定路径(并且没有尾部斜杠)的服务器地址。尝试将其更改为:

    https://milliondollarwiki.co.uk

    看看能不能解决你的问题。

    【讨论】:

    • 嗨@scott-biggart,谢谢你:) 我已经更改为https://milliondollarwiki.co.ukhttps://milliondollarwiki.co.uk/ 和代码运行的子页面,但我仍然遇到同样的错误:(
    【解决方案2】:

    是的,我设法把它修好了:)。我不得不重新创建需要一两分钟的应用程序,但这很好。奇数:-/

    【讨论】:

    • 同样的事情 - 重新创建应用程序似乎由于某种原因修复了它。
    猜你喜欢
    • 2017-10-05
    • 2016-05-20
    • 2015-07-29
    • 1970-01-01
    • 2017-12-16
    • 2016-11-25
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多