從瀏覽器呼叫 APICall API from a browser

本文可協助您直接從瀏覽器對 Azure 自訂決策服務 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. 標題頁面使用自訂決策服務來指定其文章頁面的順序。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,以傳回自訂決策服務要考量的文章 (動作) 清單。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> 是必要項目,而且自訂決策服務用來作為動作識別碼。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.