mardi 13 octobre 2015

Titanium HTTP error

I am having a website which contains vendors page. In webpage when user clicks on particular company all data relevant to vendors returns but when vendor api is called it passes a variable name x-access-token which is generally data returned when user logs in. Please have a look as following image to get idea.

enter image description here

Now I want to use the same api in my Titanium application and get all vendors in particular company. For which following mentioned code has been written by me

var url = "http://ift.tt/1WZoVAp";

var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // function called in readyState DONE (4)
        Ti.API.info('onload called, readyState = '+this.readyState);
        Ti.API.info("Status " + xhr.status);
    },
    onerror: function(e) {
        // function called in readyState DONE (4)
        Ti.API.info('onerror called, readyState = '+this.readyState);
        Ti.API.info("Status " + xhr.status);
        Ti.API.info('ERROR - '+e.error);
    },
    ondatastream: function(e) {
        // function called as data is downloaded
        Ti.API.info('ondatastream called, readyState = '+this.readyState);
        Ti.API.info("Status " + xhr.status);
    },
    onsendstream: function(e) {
        // function called as data is uploaded
        Ti.API.info('onsendstream called, readyState = '+this.readyState);
        Ti.API.info("Status " + xhr.status);
    },
    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:7000  /* in milliseconds */
});

xhr.open('GET', url);
xhr.setRequestHeader('x-access-token', userToken);
xhr.send();

And following is output for the same.

case 1, readyState = 1
case 3, readyState = 3
case 4, readyState = 4
ondatastream called, readyState = 4
Status 403
case 4, readyState = 4
onsendstream called, readyState = 4
Status 403
ondatastream called, readyState = 4
Status 403
onerror called, readyState = 4
Status 403
ERROR - HTTP error

I am getting HTTP error. Am i setting header for x-access-token in wrong way or there exists any alternate way in order to perform the same ?. Can any one help me out with this ? Any suggestion regarding the same would be highly appreciated.




Aucun commentaire:

Enregistrer un commentaire