JSON Code Sample (Video SourceType)

Bing

This topic contains a code sample that produces a JSON request for the Video SourceType. For more information Video SourceType (Bing, Version 2.x).

Requirements

  • A deployment computer with an Internet connection

  • The ability to send requests using Hyper Text Transfer Protocol (HTTP 1.1)

  • The ability to parse JavaScript

Demonstrates

These code samples demonstrate how to:

  • Send a request to the Bing JSON interface and the Video SourceType

  • Display the Bing response as results

The sample is written in JavaScript.

Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Bing API 2.1 Video Sample</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script id="searchCallback" type="text/javascript" src="">
    </script>

    <script type="text/javascript">
    
    // Replace the following string with the AppId you received from the
    // Bing Developer Center.
    var AppId = "Insert AppId here";
    
    // Bing API 2.1 code sample demonstrating the use of the
    // Video SourceType over the JSON Protocol.
    function Search()
    {
        var requestStr = "http://api.bing.net/json.aspx?"
        
            // Common request fields (required)
            + "AppId=" + AppId
            + "&Query=xbox site:microsoft.com"
            + "&Sources=Video"
            
            // Common request fields (optional)
            + "&Version=2.1"
            + "&Market=en-us"
            + "&Adult=Moderate"

            // Video-specific request fields (optional)
            + "&Video.Count=2"
            + "&Video.Offset=0"

            // JSON-specific request fields (optional)
            + "&JsonType=callback"
            + "&JsonCallback=SearchCompleted";

         var requestScript = document.getElementById("searchCallback");
         requestScript.src = requestStr;
    }

    function SearchCompleted(response)
    {
        var errors = response.SearchResponse.Errors;
        if (errors != null)
        {
            // There are errors in the response. Display error details.
            DisplayErrors(errors);
        }
        else
        {
            // There were no errors in the response. Display the
            // Video results.
            DisplayResults(response);
        }
    }

    function DisplayResults(response)
    {
        var output = document.getElementById("output");
        var resultsHeader = document.createElement("h4");
        var resultsList = document.createElement("ul");
        output.appendChild(resultsHeader);
        output.appendChild(resultsList);
    
        var results = response.SearchResponse.Video.Results;
        
        // Display the results header.
        resultsHeader.innerHTML = "Bing API Version "
            + response.SearchResponse.Version
            + "<br />Video results for "
            + response.SearchResponse.Query.SearchTerms
            + "<br />Displaying "
            + (response.SearchResponse.Video.Offset + 1)
            + " to "
            + (response.SearchResponse.Video.Offset + results.length)
            + " of "
            + response.SearchResponse.Video.Total
            + " results<br />";
        
        // Display the Video results.
        var resultsListItem = null;
        for (var i = 0; i < results.length; ++i)
        {
            resultsListItem = document.createElement("li");
            resultsList.appendChild(resultsListItem);
            resultsListItem.innerHTML = "Title: " 
                + results[i].Title 
                + "<br />"
                  
                + "SourceTitle: " 
                + results[i].SourceTitle 
                + "<br />"
                
                + "RunTime: " 
                + results[i].RunTime 
                + "<br />"
                
                + "<a href=\""
                + results[i].PlayUrl
                + "\">"
                + "PlayUrl"
                + "</a><br />"
                
                + "<a href=\""
                + results[i].ClickThroughPageUrl
                + "\">"
                + "ClickThroughPageUrl"
                + "</a><br />"
                
                + "Static Thumbnail:"
                + "<br />"
                + "<a href=\""
                + results[i].PlayUrl
                + "\">"
                + "<img src=\""
                + results[i].StaticThumbnail.Url
                + "\"></a>"
                
        }
    }

    function DisplayErrors(errors)
    {
        var output = document.getElementById("output");
        var errorsHeader = document.createElement("h4");
        var errorsList = document.createElement("ul");
        output.appendChild(errorsHeader);
        output.appendChild(errorsList);
        
        // Iterate over the list of errors and display error details.
        errorsHeader.innerHTML = "Errors:";
        var errorsListItem = null;
        for (var i = 0; i < errors.length; ++i)
        {
            errorsListItem = document.createElement("li");
            errorsList.appendChild(errorsListItem);
            errorsListItem.innerHTML = "";
            for (var errorDetail in errors[i])
            {
                errorsListItem.innerHTML += errorDetail
                    + ": "
                    + errors[i][errorDetail]
                    + "<br />";
            }
            
            errorsListItem.innerHTML += "<br />";
        }
    }
    
    </script>

</head>
<body onload="Search()">
    <div id="output"></div>
</body>
</html>
Show: