您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

将请求发送到必应拼写检查 APISending requests to the Bing Spell Check API

警告

必应搜索 API 将从认知服务迁移到必应搜索服务。Bing Search APIs are moving from Cognitive Services to Bing Search Services. 2020 年10月 30 日起,需要按照 此处所述的过程设置必应搜索的任何新实例。Starting October 30, 2020 , any new instances of Bing Search need to be provisioned following the process documented here. 在接下来的三年中,将支持使用认知服务进行预配的必应搜索 API,或者在企业协议结束后(以先发生者为准)。Bing Search APIs provisioned using Cognitive Services will be supported for the next three years or until the end of your Enterprise Agreement, whichever happens first. 有关迁移说明,请参阅 必应搜索服务For migration instructions, see Bing Search Services.

要检查文本字符串是否存在拼写和语法错误,你需要向以下终结点发送 GET 请求:To check a text string for spelling and grammar errors, you'd send a GET request to the following endpoint:

https://api.cognitive.microsoft.com/bing/v7.0/spellcheck

请求必须使用 HTTPS 协议。The request must use the HTTPS protocol.

建议所有请求都源自服务器。We recommend that all requests originate from a server. 将密钥作为客户端应用程序的一部分进行分发会为恶意第三方提供更多访问它的机会。Distributing the key as part of a client application provides more opportunity for a malicious third-party to access it. 服务器还会提供 API 的将来版本的单一升级点。A server also provides a single upgrade point for future versions of the API.

请求必须指定 text 查询参数,其中包含要检查的文本字符串。The request must specify the text query parameter, which contains the text string to proof. 尽管是可选的,但请求还应该指定 mkt 查询参数,它确定你希望结果源自其中的市场。Although optional, the request should also specify the mkt query parameter, which identifies the market where you want the results to come from. 有关可选查询参数(例如 mode)的列表,请参阅查询参数For a list of optional query parameters such as mode, see Query Parameters. 所有查询参数值都必须是 URL 编码。All query parameter values must be URL encoded.

请求必须指定 Ocp-Apim-Subscription-Key 请求头。The request must specify the Ocp-Apim-Subscription-Key header. 尽管是可选的,但仍建议还指定以下请求头。Although optional, you are encouraged to also specify the following headers. 这些标头有助于必应拼写检查 API 返回更准确的结果:These headers help the Bing Spell Check API return more accurate results:

有关所有请求头和响应头的列表,请参阅For a list of all request and response headers, see Headers.

当使用 JavaScript 调用必应拼写检查 API 时,浏览器内置的安全功能可能会阻止你访问这些标头的值。When calling the Bing Spell Check API using JavaScript, your browser's built-in security features might prevent you from accessing the values of these headers.

若要解决此问题,可以通过 CORS 代理发出必应拼写检查 API 请求。To resolve this issue, you can make the Bing Spell Check API request through a CORS proxy. 此类代理的响应有一个 Access-Control-Expose-Headers 标头,该标头筛选响应标头并使它们可供 JavaScript 使用。The response from such a proxy has a Access-Control-Expose-Headers header that filters response headers and makes them available to JavaScript.

可以轻松安装 CORS 代理,使教程应用可以访问可选的客户端标头。It's easy to install a CORS proxy to allow the tutorial app to access the optional client headers. 首先,如果尚未安装 Node.js,请先安装First, if you don't already have it, install Node.js. 然后,在命令提示符处输入以下命令。Then enter the following command at a command prompt.

npm install -g cors-proxy-server

接下来,将 HTML 文件中的必应拼写检查 API 终结点更改为: Next, change the Bing Spell Check API endpoint in the HTML file to:
http://localhost:9090/https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/

最后,运行下面的命令,启动 CORS 代理:Finally, start the CORS proxy with the following command:

cors-proxy-server

使用教程应用期间,不要关闭命令窗口;关闭窗口会导致代理停止运行。Leave the command window open while you use the tutorial app; closing the window stops the proxy. 在搜索结果下的可展开 HTTP 标头部分中,现在可以看到 X-MSEdge-ClientID 标头(以及其他标头),并验证是否对于每个请求该标头都相同。In the expandable HTTP Headers section below the search results, you can now see the X-MSEdge-ClientID header (among others) and verify that it's the same for each request.

示例 API 请求Example API request

以下显示了包含所有建议查询参数和标头的请求。The following shows a request that includes all the suggested query parameters and headers. 如果是首次调用任何必应 API,请勿添加客户端 ID 请求头。If it's your first time calling any of the Bing APIs, don't include the client ID header. 只有在以前调用过必应 API 且必应针对用户和设备组合返回了客户端 ID 的情况下,才包括客户端 ID。Only include the client ID if you've previously called a Bing API and Bing returned a client ID for the user and device combination.

GET https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?text=when+its+your+turn+turn,+john,+come+runing&mkt=en-us HTTP/1.1
Ocp-Apim-Subscription-Key: 123456789ABCDE  
X-MSEdge-ClientIP: 999.999.999.999  
X-Search-Location: lat:47.60357;long:-122.3295;re:100  
X-MSEdge-ClientID: <blobFromPriorResponseGoesHere>  
Host: api.cognitive.microsoft.com  

下面的示例展示了对上一请求的响应。The following shows the response to the previous request. 此示例还展示了必应专用响应头。The example also shows the Bing-specific response headers.

备注

由于 URL 格式和参数可能会在未另行通知的情况下有所更改,请按现状使用所有 URL。Because URL formats and parameters are subject to change without notice, use all URLs as-is. 不应依赖于 URL 格式或参数,除非另有说明。You should not take dependencies on the URL format or parameters except where noted.

BingAPIs-TraceId: 76DD2C2549B94F9FB55B4BD6FEB6AC
X-MSEdge-ClientID: 1C3352B306E669780D58D607B96869
BingAPIs-Market: en-US

{  
    "_type" : "SpellCheck",  
    "flaggedTokens" : [{  
        "offset" : 5,  
        "token" : "its",  
        "type" : "UnknownToken",  
        "suggestions" : [{  
            "suggestion" : "it's",  
            "score" : 1  
        }]  
    },  
    {  
        "offset" : 25,  
        "token" : "john",  
        "type" : "UnknownToken",  
        "suggestions" : [{  
            "suggestion" : "John",  
            "score" : 1  
        }]  
    },  
    {  
        "offset" : 19,  
        "token" : "turn",  
        "type" : "RepeatedToken",  
        "suggestions" : [{  
            "suggestion" : "",  
            "score" : 1  
        }]  
    },  
    {  
        "offset" : 35,  
        "token" : "runing",  
        "type" : "UnknownToken",  
        "suggestions" : [{  
            "suggestion" : "running",  
            "score" : 1  
        }]  
    }]  
}  

后续步骤Next steps