对Microsoft Azure Translate API的CORS支持吗? [英] CORS support for Microsoft Azure Translate API?
问题描述
这是与 Microsoft Azure Translate API现在支持CORS吗?
Does the Microsoft Azure Translate API support CORS now?
推荐答案
Microsoft翻译器文本API 似乎至少现在至少支持CORS,因为它似乎至少在响应中发送了Access-Control-Allow-Origin
标头:
The Microsoft Translator Text API appears to at least minimally now support CORS, in that it does at least seem to send the Access-Control-Allow-Origin
header in responses:
$ 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
可以进行测试-但是,如果您这样做,我认为您会发现它可以正常工作:
I don’t personally have a valid appid
to test with—but if you do, I think you’ll find it’ll work:
- 如果您从 https://docs.microsofttranslator.com/使用
GET
端点text-translate.html 文档 - 如果您使用
appid
参数而不是Authorization
请求标头进行身份验证
- if you use
GET
endpoints from the https://docs.microsofttranslator.com/text-translate.html docs - if you authenticate using the
appid
parameter instead of theAuthorization
request header
如果可能也适用于 https://docs. microsofttranslator.com/text-translate.html POST
终结点-只要您的请求不使用Authorization
请求标头或设置Content-Type
.
If may also work for the https://docs.microsofttranslator.com/text-translate.html POST
endpoints—as long as your requests don’t use the Authorization
request header or set a Content-Type
.
这些标头的问题在于,它们会触发浏览器执行预检OPTIONS
请求:
The problem with those headers is, they’ll trigger browsers to do a preflight OPTIONS
request:
https://developer.mozilla.org/en -US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
调用那些触发预检的API端点的问题是,它们似乎并没有以使浏览器将预检视为成功的方式来响应OPTIONS
请求.
And the problem with making calls to those API endpoints that trigger a preflight is, they don’t seem to respond to OPTIONS
requests in a way that’ll cause browsers to see the preflight as a success.
在 https://docs.microsofttranslator.com/text- translation.html#!/default/post_TranslateArray 我注意到文档说端点期望POST
带有application/xml
或text/xml
Content-Type
,因此,如果该端点不响应飞行前
At https://docs.microsofttranslator.com/text-translate.html#!/default/post_TranslateArray I notice the docs say that endpoint expects a POST
with an application/xml
or text/xml
Content-Type
, so if that endpoint doesn’t respond to preflight OPTIONS
in the right way, that one won’t work.
那是因为在请求中添加Content-Type: application/xml
或Content-Type: text/xml
标头肯定会触发浏览器在POST
之前进行预检OPTIONS
.
That’s because adding an Content-Type: application/xml
or Content-Type: text/xml
header to a request will definitely trigger browsers to do a preflight OPTIONS
before the POST
.
这篇关于对Microsoft Azure Translate API的CORS支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!