Creating TVML with php script, using XMLHttpRequest() to get the file contents

I recently started creating a tvOS channel for my company. I did not have experence with iOS devices, Swift, or Obj-C before, however, I have started studying these to create the apps to be placed on Apple's App store.


After looking through the TVML stuff and all the different templates that are provided, I have a design that I want to implement. I was think I could use a php script to pull all of our different videos and episodes from our database and server, this would give the application a real time update. As we update our database, the script would in turn update the TVML/XML file with the new content in the correct areas. I will block out the url's being used but post my XMLHttpRequest code:


var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "http://branches.globalsportsman.com/******", false);
    xhttp.send();
    return xhttp.responseText;
}



This would be sync. request call, pulling back the response body as string. Which I then pass into presenter.js here:

var doc = Presenter.parser.parseFromString(resource, "application/xml");
        return doc;


I am getting the error that resource, here, is an invalid parameter. The systems works correctly if the TVML/XML is created by hand and sent in as a string. Maybe someone could give me insight on what is going wrong here. The php script is giving correctly formatted XML in browser, using the correct TVML syntax, I pastsed in the same XML into a variable and returned it to resource and it was fine.

I'm having similar problem, but to download a blob file (an image byte array). There is no enough docs in TVJS, so maybe you can get some help trying this complete implementation, that will print out in Safari more info:


var xhr = new XMLHttpRequest();
    xhr.responseType = "blob"; // arraybuffer
    xhr.onabort = function() {
       
    };
    xhr.onerror = function () {
        console.log( xhr.error ); 
    };
    xhr.onloadend = function () {
       
    };
    xhr.onloadstart = function () {
       
    };
    xhr.ontimeout = function () {
        console.log( "Request timed out after " + xhr.timeout );  
    };
    xhr.onload = function () {
       
        console.log( xhr.getAllResponseHeaders () );
        console.log( xhr.metrics );
        console.log( xhr.response );
        if(xhr.response) {
            localStorage.setItem("mxm_image_arraybuffer", xhr.response);   
        }
        callback(xhr.response);
    };
    /xhr.addEventListener("load", function() {
       
        console.log( "RESPONSE ");
        console.log( xhr.getAllResponseHeaders () );
        console.log( xhr.response );
       
        callback(xhr.response);
    }, false);*/
    xhr.open("GET", path, 0); //async 0, sync 1
    xhr.send(null);


Give it a try!

I use the following code to do exactly what you want to do.


var templateXHR = new XMLHttpRequest();

var url = "yourdomain.com/path/to/script/";
var mode = modeFlag || 'normal';


templateXHR.responseType = "document";
templateXHR.addEventListener("load", function() {
    var document = templateXHR.responseXML;
    document.addEventListener("select", Presenter.load.bind( Presenter ) );
    if( 'modal' !== mode ) {
        Presenter.pushDocument( document );
    } else {
        Presenter.modalDialogPresenter( document );
    }
}, false);
templateXHR.open("GET", url, true);
templateXHR.send();
return templateXHR;


Make sure you change your PHP header to be XML if you're loading XML or JS if you're loading JS.


Hope that helps.


- Rheinard

Do you had the chance to use XMLHttpRequest for something different than a text or xml responseType? In my case I cannot get rid of a binary response - take a look at question https://forums.developer.apple.com/thread/26643

Creating TVML with php script, using XMLHttpRequest() to get the file contents
 
 
Q