Sunday, April 15, 2012

Titanium HTTPClient, Sending a complex object

If you need to send a complex object on a request through the Titanium HTTPClient, an object that has more then a single level of data, for example:
var data_to_send={
   user:{name:'Gilad', l_name:'Manor'}
}
Passing this object, as is, to the HTTPClient will result in the server getting the sub object as a string, instead of an object.
To have this properly sent, you will need to wrap the data with JSON.stringify
i.e.

client.setRequestHeader('Content-Type', 
               'application/json; charset=utf-8');
client.send(JSON.stringify(data));




1 comment:

  1. var uname=tname.value.toString(); var pass=tsurname.value.toString();
    var jsonobj= {


    "username" :'"'+uname+'"',
    "password" : '"'+pass+'"'
    };
    var client= Ti.Network.createHTTPClient({ onload:function(e){}
    client.setRequestHeader('content-type', 'JSON');
    client.open('POST',url);
    client.send(JSON.stringify(jsonobj));
    when i am passing hardcore values in the jsonobj, it completly works fine, but as i am trying to take values from the textfields it does not. blank value are send to web services. can any one help me on this, how can i add user input value from textfield and add to the object and send to the apigee baas.

    ReplyDelete

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.