【问题标题】:Making a call to the UBER api using $.Ajax使用 $.Ajax 调用 UBER api
【发布时间】:2019-06-03 18:53:24
【问题描述】:

这里是 uber api 文档:

curl -H 'Authorization: Token <SERVER_TOKEN>' \
     -H 'Accept-Language: en_US' \
     -H 'Content-Type: application/json' \
     'https://api.uber.com/v1.2/estimates/price?start_latitude=37.7752315&start_longitude=-122.418075&end_latitude=37.7752415&end_longitude=-122.518075'

这是我正在使用的代码:

jQuery.ajax({
            type: "GET",
            url: "https://api.uber.com/v1.2/estimates/price?start_latitude=37.7752315&start_longitude=-122.418075&end_latitude=37.7752415&end_longitude=-122.518075",
            beforeSend: function(xhr) {

                xhr.setRequestHeader("Authorization", "Token <myToken>");
                xhr.setRequestHeader('Accept-Language', 'en_US'); 
                xhr.setRequestHeader('Content-Type', 'application/json');
                xhr.setRequestHeader("Access-Control-Allow-Origin","*");

            },
            success: function(data){
               console.log(data);
            }
        });

我收到的错误是“CORS 策略已阻止在 'https://api.uber.com/v1.2/estimates/price?start_latitude=37.7752315&start_longitude=-122.418075&end_latitude=37.7752415&end_longitude=-122.518075' 处从源 'null' 访问 XMLHttpRequest:Access-Control-Allow 不允许请求标头字段 access-control-allow-origin - 预检响应中的标头。”

我认为这会在我的标题中处理,但我在某个地方出错了。

提前致谢!

编辑:下面的正确答案

【问题讨论】:

标签: javascript jquery api curl


【解决方案1】:

根据 Uber 文档,该 API 支持 CORS 以验证将使用您的服务器令牌调用 Uber API 的来源。

CORS 验证到达 API 的请求的源域,作为一种机制来防止不允许的来源使用您的 API。然而,这不是一种身份验证方案。你可以在这里阅读更多信息CORS

要解决此问题,您需要在应用设置的安全部分中将您发出请求的源域配置到优步应用的配置中:Cross-Origin Resource Sharing

【讨论】:

  • 是的,你完全正确。我只需要对这个主题做更多的研究。感谢您的回复所花费的时间和精力!
猜你喜欢
  • 1970-01-01
  • 2016-08-18
  • 2016-12-02
  • 1970-01-01
  • 2022-09-24
  • 2014-09-14
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多