Saturday, December 17, 2011

Titanium Appcelerator Web services method like AJAX

https://wiki.appcelerator.org/display/guides/HTTPClient+and+the+Request+Lifecycle


GET

var url = "https://www.appcelerator.com" ;
var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // this function is called when data is returned from the server and available for use
        // this.responseText holds the raw text return of the message (used for text/JSON)
        // this.responseXML holds any returned XML (including SOAP)
        // this.responseData holds any returned binary data
        Ti.API.debug(this.responseText);
        alert('success');
    },
    onerror: function(e) {
        // this function is called when an error occurs, including a timeout
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000  /* in milliseconds */
});
xhr.open("GET", url);
xhr.send();  // request is actually sent with this statement




POST


var xhr = Ti.Network.createHTTPClient();

xhr.onload = function(e) {
    //handle response, which at minimum will be an HTTP status code
};

xhr.open('POST','http://www.myblog.com/post.php' );
xhr.send({
    title:'My awesome blog',
    body:'Today I met Susy at the laundromat.  Best day EVAR\!'
});




Ready State Monitoring


var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // function called in readyState DONE (4)
        Ti.API.info('onload called, readyState = '+this.readyState);
    },
    onerror: function(e) {
        // function called in readyState DONE (4)
        Ti.API.info('onerror called, readyState = '+this.readyState);
    },
    ondatastream: function(e) {
        // function called as data is downloaded
        Ti.API.info('ondatastream called, readyState = '+this.readyState);
    },
    onsendstream: function(e) {
        // function called as data is uploaded
        Ti.API.info('onsendstream called, readyState = '+this.readyState);
    },
    onreadystatechange: function(e) {
        switch(this.readyState) {
            case 0:
                // after HTTPClient declared, prior to open()
                // though Ti won't actually report on this readyState
                Ti.API.info('case 0, readyState = '+this.readyState);
            break;
            case 1:
                // open() has been called, now is the time to set headers
                Ti.API.info('case 1, readyState = '+this.readyState);
            break;
            case 2:
                // headers received, xhr.status should be available now
                Ti.API.info('case 2, readyState = '+this.readyState);
            break;
            case 3:
                // data is being received, onsendstream/ondatastream being called now
                Ti.API.info('case 3, readyState = '+this.readyState);
            break;
            case 4:
                // done, onload or onerror should be called now
                Ti.API.info('case 4, readyState = '+this.readyState);
            break;
        }
    },
    timeout:5000  /* in milliseconds */
});
xhr.open("GET", 'http://training.appcelerator.com.s3.amazonaws.com/atp_doc.pdf');
xhr.send();  // request is actually sent with this statement



No comments:

Post a Comment