Sending AJAX requests in Samsung Smart TV

Samsung Smart TV
2 minutes read

In this article, we will be discussing about sending AJAX requests from the smart TV and getting remote contents to the application. Most of today’s smart TVs are internet connected devices, that frequently updates remote content from the internet for it’s applications. Most of the contents are fetched asynchronously using AJAX requests. This enables the TV devices to fetch content without disturbing the normal flow of the application thereby enhancing the user experience.

Small history
In the olden days of the internet, the server requests are blocking requests. Once a web request is issued, the front end is blocked until the server response is received. This will render the application useless during the course of the request. In order to solve this problem, the AJAX specification is formed. This will send the web request asynchronously and the user can use the app during the course of the web request. The overall user experience has increased. The same path is followed by Samsung smart TVs for retrieving contents from the remote servers.



The XMLHttpRequest object
Usually the AJAX object is provided by the common API library provided by samsung. The developer can also make use of the XMLHttpRequest class to send AJAX requests directly. The XMLHttpRequest is a standard implementation of AJAX. This also prevents the user to import the supplied common library of Samsung smart TV or any third party libraries for making AJAX calls.

Code snippet and explanation
In this article we will cover the XMLHttpRequest based AJAX calls since they are the standard. The following code shows a JavaScript function that accepts an URL input and retrieves it contents and prints them to the console output.

function fetchRemoteContents = function(remoteURL) {
    var XHRObj = new XMLHttpRequest();
    
    if (XHRObj) {
        XHRObj.onreadystatechange = function() {
            if (XHRObj.readyState == 4) {
                console.log(XHRObj.response); // Prints the obtained response to the console
            }
        }
        XHRObj.open("GET", remoteURL, true);
        XHRObj.send(null);
    } else {
        alert("Failed to create XHR");
    }
}



The following are the observations from the above code

  • The XMLHttpRequest Object can be initialized using the new operator
  • The onreadystatechange callback is triggered when the request response is received
  • The status of the response is checked inside the onreadystatechange callback using the readyState property
  • The readyState property of 4 indicates a successful request
  • Further app decisions are taken based on the readyState value of the XMLHttpRequest object.
  • In the open function call, the type of request (GET, PUT, POST) etc can be specified
  • The send function accepts the data that is to be send along with the request (especially for POST requests)
  • GET requests do not send any body data to the server (hence null inside the send function)

Using the XMLHttpRequest class, native AJAX calls can be sent from the Samsung smart TV applications and remote contents can be fetched asynchronously. Asynchronous request fetch the remote data in the background and thus do not block the front end operations, thereby enhancing the user experience. Our next article will be focussed on creating a video application, that downloads the remote content like video URLs and then plays the video content based on user actions. Stay tuned.




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.