Question

This is an example of how I currently make an api call using titanium:

var url = "http://www.appcelerator.com";
 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: " + this.responseText);
         alert('success');
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         alert('error');
     },
     timeout : 5000  // in milliseconds
 });
 // Prepare the connection.
 client.open("GET", url);
 // Send the request.
 client.send();

The trouble is by doing it this way, I am only able to access the object in the onload call back function.

I can't for example do this:

//snippet
    var someObject;

    onerror : function(e) {

      someObject = this.responseText;

             },

//end    

function useObject(someObject){

alert(someObject);


}

Using jquery AJAX I would be able to do this, like this:

 $.ajax({
            type: "POST",
            url: 'someurl',
            data: param = "",
            contentType: "application/json; charset=utf-8",
            success: self.useObject,
            error: errorFunc
        });

Once the response is received, pass it to the success object.

How can I do the equilent in Titanium, given that it does not use Jquery.

Was it helpful?

Solution

I don't fully understand what you are trying to achieve ,but try something like:

var onLoad = function(e) {
   console.log(this.responseText);
};

var client = Ti.Network.createHTTPClient({
     onload: onLoad
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top