javascriptqmlblackberry-cascades

Unable to load JSON DataSource over https in BlackBerry Cascades


Loading my JSON DataSource through https seems to be failing. It works when I load the url in a browser on my BlackBerry 10 device but fails when I try to use that url in Qml.

This is what my DataSource object looks like:

DataSource {
    id: dsTitles
    source: ""
    type: DataSourceType.Json
    onDataLoaded: {
        if (Common.hasError(updateError, data, "Failed to load data. Please check your connection and try again.")) {
            console.log("Data contains error");
            navigationPane.pop();
            return;
        }
        loadedTitles = true;
        Code.loadDropDown(data, ddTitle, "title", "titleId")
        Code.hideLoadIndicator();
        if (updateProfile && ddTitle.selectedValue == null) {
            Code.setDropDownOptionByValue(ddTitle, profile.userTitleId);
        }
    }
    onError: {
        console.log("Failed to load titles: " + errorMessage);
    }

Any https JSON web service can be used as an example of this problem.


Solution

  • It is possible to use XMLHttpRequest to load data from JavaScript. I have created the following two functions:

    function loadJsonDataList(value, dataModel) { for (var i = 0; i < value.length; i ++) { dataModel.insert(value[i]); } return value; }

    function loadData(url, onComplete) { var request = new XMLHttpRequest();

    request.open("GET", url);
    request.send();
    
    request.onreadystatechange = function() {
        if (request.readyState === 4 && request.status === 200) {
            onComplete(JSON.parse(request.responseText));
        } else {
    
            onComplete(request.responseText);
        }
    };
    

    }

    You can use the functions above as follows:

    function getData2() 
    {
        console.log("Get data called");
        Common.loadData("https://mywebservice/here/someFunction", getDataCallBack);
    }
    
    function getDataCallBack(dataObject) 
    {
        console.log("Get data callback called");
        Common.loadJsonDataList(dataObject, lstViewItems.dataModel);
    }
    

    Currently it seems like Qml DataSource objects are unable to load SSL web service data, however it is possible to load it manually as stated above. I will try to keep an eye on the issue and update the answer if they do eventually fix it or if another answer can explain how to use DataSource this way.