2017-03-19 41 views
0

第一次在這裏問一個問題。我是這個初學者,但我真的很難對付我面臨的問題。與CORS的優步API問題

瀏覽器在使用中:

  • Safari和Firefox(包括在Mac OS塞拉利昂)
  • 火狐(Linux的 - 的Ubuntu 16.04.2)

我註冊成爲尤伯杯Developer和已在儀表板中註冊了一個應用程序。我現在只使用服務器令牌進行身份驗證。在儀表板,我已經在App爲CORS(可選URI的CORS支持)的「權限」選項卡中設置以下條目:

http://localhost:8000     <-- web server in my PC 
https://subdomain.mydomain.com   <--- remote web server 

幾個月前,我使用HTML,CSS創建一個Web應用程序和JS(帶有Jquery v2.2.4)可以使用Ride Estimates API,並能夠成功地爲我所在地區的許多位置報告數據。不知何故,它不再有效。我試圖解決這個問題並改進功能。不過,由於以前不存在的CORS問題,我無法超越對API的初始查詢。

我的API網址是:

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服務器,然後加載我的網頁中的任何b羅塞爾從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瀏覽器的檢查來看,下設網絡/資源的地區,我看到從獲得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 

在Firefox用於Linux我有時不要得到語法錯誤;我似乎總是把它放在Mac瀏覽器上。在Linux中,當我得到這個錯誤,然後點擊「Edit and Resend」Headers按鈕(重新發送Headers但沒有真正編輯Headers),Syntax Error就消失了,Response文本實際上顯示了Uber API Object在那裏......但我仍然在控制檯日誌上看到CORS Blocked消息。我真的不明白這是爲什麼,但似乎是矛盾的。最後,我無法使用API​​數據,在幾個月前使用相同的方法,我可以獲得幾十個位置。

我在類似的問題尋找答案,但到目前爲止沒有發現任何適用於我的情況。任何幫助將不勝感激。變得非常沮喪......真的停留在這裏。

+0

我發現在Safari(Mac)中,如果我進入「開發」菜單並單擊以啓用「禁用跨源限制」,那麼在該瀏覽器中不再存在與CORS有關的問題......但這不是爲了成爲解決方案,我相信。 – hutchit

+0

我認爲問題在於Uber API沒有按預期包含CORS頭文件。看起來像一個錯誤 - 我已經報告過,並會在解決後跟進。 –

回答

1

此問題是由API未正確包含標頭引起的。此問題已解決,並且api現在按預期工作。另外,如果在請求中指定了一個源,則只允許返回允許源標頭。

+0

感謝您的回覆...我的網絡應用程序正在工作。 – hutchit