第一次在这里问问题。我是这方面的初学者,但我真的对我面临的问题感到困惑。
正在使用的浏览器:
我注册为优步开发人员,并在仪表板上注册了一个应用程序。目前,我只使用服务器令牌进行身份验证。在仪表板中,我在App的“授权”选项卡中设置了以下条目(用于CORS支持的可选URI ):
http://localhost:8000 <-- web server in my PC
https://subdomain.mydomain.com <--- remote web server几个月前,我使用HTML、CSS和JS (使用jQueryv2.2.4)创建了一个web应用程序,它可以使用Ride估测API,并能够成功地报告我所在地区许多位置的数据。不知何故,它不再起作用了。我正在努力修复这个问题,并提高功能。但是,由于以前不存在的CORS问题,我无法通过对API的初始查询。
我的API URL是:
https://api.uber.com/v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********}当我将其粘贴到浏览器的地址栏中时,就会得到有效的JSON:
{"prices":[{"localized_display_name":"uberX","distance":1.58,"display_name":"uberX","product_id":"811c3224-5554-4d29-98ae-c4366882011f","high_estimate":3,"surge_multiplier":1.0,"minimum":2,"low_estimate":2,"duration":420,"estimate":"2-3\u00a0$","currency_code":"USD"},{"localized_display_name":"X English","distance":1.58,"display_name":"X English","product_id":"8fe2c122-a4f0-43cc-97e0-ca5ef8b57fbc","high_estimate":4,"surge_multiplier":1.0,"minimum":3,"low_estimate":3,"duration":420,"estimate":"3-4\u00a0$","currency_code":"USD"},{"localized_display_name":"uberXL","distance":1.58,"display_name":"uberXL","product_id":"eb454d82-dcef-4d56-97ca-04cb11844ff2","high_estimate":4,"surge_multiplier":1.0,"minimum":3,"low_estimate":3,"duration":420,"estimate":"3-4\u00a0$","currency_code":"USD"},{"localized_display_name":"Uber Black","distance":1.58,"display_name":"Uber Black","product_id":"ba49000c-3b04-4f54-8d50-f7ae0e20e867","high_estimate":6,"surge_multiplier":1.0,"minimum":4,"low_estimate":4,"duration":420,"estimate":"4-6\u00a0$","currency_code":"USD"},{"localized_display_name":"Uber SUV","distance":1.58,"display_name":"Uber SUV","product_id":"65aaf0c2-655a-437d-bf72-5d935cf95ec9","high_estimate":7,"surge_multiplier":1.0,"minimum":5,"low_estimate":5,"duration":420,"estimate":"5-7\u00a0$","currency_code":"USD"}]}然后继续在网页中设置JS (w/ JQuery)代码.
var url = "https://api.uber.com/v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********}";
$.getJSON(url, function(result){
console.log(result);
});将HTML和JS上传到我的远程web服务器,然后在我的任何浏览器中加载网页,就会从Uber API中获得200种状态。但是,控制台日志显示CORS阻塞了我的请求(问题#1):
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.uber.com/v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********}. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).然后,在两个Mac浏览器的检查视图中,在Network / Resources区域下,我看到了来自GET请求的200状态消息。但是,连同响应消息(问题2):
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data请求头是:
GET /v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********} HTTP/1.1
Host: api.uber.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://subdomain.domain.com/Uber/index.html
Origin: https://subdomain.domain.com
Connection: keep-alive响应头是:
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 19 Mar 2017 22:26:31 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Content-Geo-System: wgs-84
Content-Language: en
X-Rate-Limit-Limit: 2000
X-Rate-Limit-Remaining: 1998
X-Rate-Limit-Reset: 1489964400
X-Uber-App: uberex-nonsandbox, optimus, migrator-uberex-optimus
Strict-Transport-Security: max-age=604800
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Encoding: gzip在Linux的Firefox中,我有时不会得到语法错误;我似乎总是在Mac浏览器上获得语法错误。在Linux中,当我得到该错误时,单击“编辑和重新发送”标题按钮(重新发送标头,但没有真正编辑标头),语法错误就消失了,响应文本实际上显示了应该在那里的Uber API对象.但是我仍然在控制台日志上看到CORS阻塞的消息。我真的不明白这是为什么,但这似乎是矛盾的。最后,我无法使用API数据,使用几个月前相同的方法,我可以得到几十个位置的API数据。
我曾在类似的问题中寻找答案,但到目前为止还没有发现适用于我的情况。任何帮助都将不胜感激。非常沮丧..。真的被困在这里了。
发布于 2017-03-20 22:20:11
此问题是由API未正确包含标头造成的。这个问题已经解决,api现在正在按预期工作。此外,只有在请求中指定了原点时,才会在响应中返回允许源标头。
https://stackoverflow.com/questions/42893214
复制相似问题