Microsoft Azure Translate API现在是否支持CORS?
答案 0 :(得分:1)
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
个端点
appid
参数而不是Authorization
请求标头如果可能也适用于https://docs.microsofttranslator.com/text-translate.html POST
个端点 - 只要您的请求不使用Authorization
请求标头或设置{{ 1}}。
这些标题的问题是,它们会触发浏览器执行预检Content-Type
请求:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
调用那些触发预检的API端点的问题是,它们似乎没有以某种方式响应OPTIONS
请求,导致浏览器看到预检成功。< / p>
在https://docs.microsofttranslator.com/text-translate.html#!/default/post_TranslateArray我注意到文档说端点需要OPTIONS
POST
或application/xml
text/xml
,所以如果该端点没有响应以正确的方式预检Content-Type
,那将无法正常工作。
这是因为在请求中添加OPTIONS
或Content-Type: application/xml
标头肯定会触发浏览器在Content-Type: text/xml
之前执行预检OPTIONS
。