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

从浏览器调用 APICall API from a browser

本文可帮助你直接从浏览器调用 Azure 自定义 AI 决策服务 API。This article helps you make calls to the Azure Custom Decision Service APIs directly from a browser.

确保先注册应用程序Be sure to Register your application, first.

让我们开始吧。Let's get started. 应用程序建模为具有首页,该页面链接到多个文章页面。Your application is modeled as having a front page, which links to several article pages. 首页使用自定义 AI 决策服务来指定其文章页面中的排序。The front page uses Custom Decision Service to specify the ordering of its article pages. 将下面代码插入首页的 HTML 标头:Insert the following code into the HTML head of the front page:

// Define the "callback function" to render UI
<script> function callback(data) { … } </script>

// call Ranking API, after callback() is defined
<script src="https://ds.microsoft.com/api/v2/<appId>/rank/<actionSetId>" async></script>

data 参数包含要呈现的 URL 排名。The data argument contains the ranking of URLs to be rendered. 有关详细信息,请参阅 API 参考For more information, see the reference API.

要处理热门文章的用户单击,请在首页上调用以下代码:To handle a user click on the top article, call the following code on the front page:

// call Reward API to report a click
$.ajax({
    type: "POST",
    url: '//ds.microsoft.com/api/v2/<appId>/reward/' + data.eventId,
    contentType: "application/json" })

此处,datacallback() 函数的参数。Here, data is the argument to the callback() function. 可在此教程中找到实现示例。An implementation example can be found in this tutorial.

最后,需要提供操作集 API,这将返回自定义 AI 决策服务考虑的文章(操作)列表。Finally, you need to provide the Action Set API, which returns the list of articles (actions) to be considered by Custom Decision Service. 将此 API 实现为 RSS 源,如下所示:Implement this API as an RSS feed, as shown here:

<rss version="2.0">
<channel>
   <item>
      <title><![CDATA[title (possibly with url) ]]></title>
      <link>url</link>
      <pubDate>Thu, 27 Apr 2017 16:30:52 GMT</pubDate>
    </item>
   <item>
       ....
   </item>
</channel>
</rss>

此处,每个顶级 <item> 元素各描述一篇文章。Here, each top-level <item> element describes an article. <link> 是必需的,被自定义 AI 决策服务用作操作 ID。The <link> is mandatory and is used as an action ID by Custom Decision Service. 如果有 15 篇以上的文章,指定 <date>(采用标准 RSS 格式)。Specify <date> (in a standard RSS format) if you have more than 15 articles. 使用 15 篇最新文章。The 15 most recent articles are used. <title> 是可选的,用于为文章创建与文本相关的功能。The <title> is optional and is used to create text-related features for the article.

后续步骤Next steps

  • 通读教程以获取更深入的示例。Work through a tutorial for a more in-depth example.
  • 请参阅 API 参考,详细了解提供的功能。Consult the reference API to learn more about the provided functionality.