对Microsoft Azure Translate API的CORS支持吗? [英] CORS support for Microsoft Azure Translate API?

查看:66
本文介绍了对Microsoft Azure Translate API的CORS支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与 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&#xD;
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:

  • if you use GET endpoints from the https://docs.microsofttranslator.com/text-translate.html docs
  • if you authenticate using the appid parameter instead of the Authorization 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/xmltext/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/xmlContent-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆