Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Problem for pass json result from onload function to variable?

$
0
0

Hi !

I retrieve Json data on a url using createClientHtpp() and I use the callback function to pass the son result.

function SearchDataJson(url, callback) {
 
    var successHandler = function () {  
        var jsonData = JSON.parse(this.responseText); 
        callback(jsonData);
    };
 
 
    var errorHandler = function () { 
        alert("connexion error");
        dataJson="error";  
    };
 
    var loginReq = Titanium.Network.createHTTPClient({
        validatesSecureCertificate: false,
        onload: successHandler,
        onerror: errorHandler,
        timeout: 5000
    });  
    // Prepare the connection.       
    var authstr = 'Basic ' + Ti.Utils.base64encode('admin' + ':' + '3edc4rfv');
    loginReq.open('GET', url);   
    //loginReq.setRequestHeader("Content-Type", "application/json");
    //loginReq.setRequestHeader("Accepts","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
    loginReq.setRequestHeader('Authorization', authstr);
    // Send the request.
    loginReq.onload._isRunning = true;
    loginReq.send();
 
};
 
module.exports = SearchDataJson;
Then, I sort my data and create a TableView.
function ApplicationTableView() {
 
    var win = Ti.UI.createWindow({
        title:"States Links",
        backgroundColor:'white'
    });
 
    var url = "https://test";
 
    function parseData(jsonData){
 
        //create a Table View
        var tableView = Ti.UI.createTableView({
            headerView : Ti.UI.createView({ height: 20 }),
        });
 
        var uplinks = jsonData.uplinks;
 
        for (i = 0; i < jsonData.uplinks.length; i++) {
            link = uplinks[i].data.name;
            console.log(link);
 
            //create data
            //create Section for the link
            var section = Ti.UI.createTableViewSection({ headerTitle: link});
            section.add(Ti.UI.createTableViewRow({ title: 'Enable', backgroundColor: "#4ACAB4"}));
            section.footerView = Ti.UI.createView({ height: 20 });
            //click : open new windows
            section.addEventListener('click', function() {
                //containingTab attribute must be set by parent tab group on
                //the window for this work
                window.containingTab.open(Ti.UI.createWindow({
                    title: L('Informations Link 1'),
                    backgroundColor: 'white'
                }));
            });
            tableView.add(section);
        }
        win.add(tableView);
    }
But, I can not view the result. The window is blank, I think that it is displayed before parseData be execute.
Thank you in advance and sorry for my english.

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>