這是一個與https://social.msdn.microsoft.com/Forums/en-US/6e856136-9a39-4b98-a53d-7f8bce08e3a6/cors-support-for-bing-translate-api相關的問題對Microsoft Azure Translate API的CORS支持?
Microsoft Azure Translate API現在是否支持CORS?
這是一個與https://social.msdn.microsoft.com/Forums/en-US/6e856136-9a39-4b98-a53d-7f8bce08e3a6/cors-support-for-bing-translate-api相關的問題對Microsoft Azure Translate API的CORS支持?
Microsoft Azure Translate API現在是否支持CORS?
的Microsoft Translator Text API似乎至少最低限度現在支持CORS,因爲它至少似乎發送Access-Control-Allow-Origin
頭的對策:
$ curl -i -H 'Origin: http://example.com' \
'https://api.microsofttranslator.com/v2/http.svc/Translate?appid=foo&text=hello&from=en&to=de'
HTTP/1.1 400 Bad Request
Content-Length: 220
Content-Type: text/html; charset=utf-8
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-MS-Trans-Info
X-MS-Trans-Info: 0642.V2_Rest.Translate.4E779D02
Date: Wed, 30 Aug 2017 09:07:34 GMT
<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: appId</p><p>Message: Invalid appId
Parameter name: appId</p><code></code><p>message id=0642.V2_Rest.Translate.4E779D02</p></body></html>
我個人並不具有有效appid
測試與 - 但如果這樣做,我想你會發現它會工作:
GET
端點從https://docs.microsofttranslator.com/text-translate.html文檔appid
參數,而不是Authorization
請求頭。如果可以也爲https://docs.microsofttranslator.com/text-translate.htmlPOST
端點,作爲工作,只要你的請求不使用Authorization
請求頭或設置Content-Type
。
與頭的問題是,他們會觸發瀏覽器做了預檢OPTIONS
要求:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
並與撥打電話,以觸發預檢是那些API端點的問題,他們不似乎不會以某種方式響應OPTIONS
請求,這會導致瀏覽器將預檢視爲成功。
在https://docs.microsofttranslator.com/text-translate.html#!/default/post_TranslateArray我注意到文檔說端點希望與application/xml
或text/xml
Content-Type
一個POST
,所以如果端點不響應以正確的方式以預檢OPTIONS
,一個將無法正常工作。
這是因爲添加Content-Type: application/xml
或Content-Type: text/xml
頭的要求肯定會觸發瀏覽器POST
前做預檢OPTIONS
。
正確!我正在使用標題'Authorization'。該文檔應該提到你剛纔所說的一切 – seza443
乾杯 - 很高興這裏的信息有所幫助。就文檔而言,當我在那裏查找時,根本找不到關於CORS支持的任何信息,或者如何從瀏覽器中運行的前端JavaScript中調用那些端點。所以,我認爲這些文檔應該比現在提到的要多得多...... – sideshowbarker