2

I have used Ti.Network.createHTTPClient in Titanium and see that the control goes neither inside onLoad nor onError. What could be the reason?

 var loader = Titanium.Network.createHTTPClient();
       loader.onload = function() { 
      alert("Hello");   
        }  
      loader.onError = function(e) 
        alert("Error: " + e.error);
     }

4 Answers4

1

Add these 2 lines to make it work! You did not send the request, nor did you send the URL

// add url in here 
loader.open("GET",'[URL HERE]'); 
// Send the request.
loader.send();
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
0
var xhrSitelogin = Titanium.Network.createHTTPClient();
xhrSitelogin.open('POST', webservice_url);
xhrSitelogin.send({
method : "userlogin",
username : username,
password : password
});
xhrSitelogin.setTimeout(10000);

xhrSitelogin.onerror = function() {
showAlertBox('Service timed out. Please try again.');
//Hide Indicator
};
xhrSitelogin.onload = function() {

  alert(this.responseText);
   //RESPONSE RECEIVED
};

Vote Up or mark best if you consider it help full.

Ali
  • 835
  • 1
  • 10
  • 23
0

Hi dosth try with this am not sure it will work if it work i will be happy

var taskRequest = Titanium.Network.createHTTPClient();

    var api_url = 'http://myawesomeapi.heroku.com/users/' + 

Ti.App.Properties.getString("userID") + '/tasks';

    taskRequest.onload = function() {

        var tasks = [];

        // code populating the tasks array

        alert(tasks);

        callback( tasks ); // invoke the callback
    }

    taskRequest.open('GET', api_url, false);

    taskRequest.setRequestHeader('Content-Type', 'application/json');

    taskRequest.send();

<....>

Nagaraja
  • 581
  • 1
  • 4
  • 12
0
loader.open("POST/GET","URL");
loader.onload(response){
//get the response
console.log(this.responseText);

};
loader.send();
 Use this pattern.

If you need to set any header then use loader.setRequestHeader("Content-Type", "application/json; charset=utf-8"); after the open()/before onload() and send()

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Mashqur
  • 9
  • 2