Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with POST JSON data Titanium

I want to post JSON data using HTTP request. I have read the official docs and I am working according to them. I am using the following code:

var xhrpost = Ti.Network.createHTTPClient();

    xhrpost.onload = function(){
        activityIndicator.hide();
        alert('Posted successfully');
        alert(JSON.stringify(this.responseText));
    }

    var posturl = 'http://qudova.com/api.php';

    xhrpost.open('POST', posturl);
    xhrpost.setRequestHeader("Content-Type", "application/json");
    xhrpost.setRequestHeader('charset','utf-8');
        var params = {
        ProjectID : picked_prj, 
        RoleID : picked_rol,
        FirstName: first.value,
        LastName: last.value,  
        Phone: phone.value,
        Email: email.value,
        City: city.value,
        State: stat_drp.getSelectedRow(0).title,
        Zip: zip.value,
        Notes: notes.value,
    };
    xhrpost.send(params);

If this is the correct way to post the JSON data. How would I check that data is posted ?? Will the url contain the posted data ??

I am getting null in the following alert that I added in the onload event.

alert(JSON.stringify(this.responseText));

I am working on Windows 7 , Testing on Android 4.2.2 .... Thanks in Advance.

like image 259
nadeem gc Avatar asked Mar 25 '13 16:03

nadeem gc


1 Answers

When you set Content-Type to json, you need to stringify the input.

var xhr = Ti.Network.createHTTPClient();

xhr.open('POST', url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader('charset','utf-8');
xhr.send(JSON.stringify({
    prop: 'string',
    data: {
        embeddedProp: 1234
    }
}));

Tried this out and it worked, wasn't able to find it in the documentation.

like image 95
joewright Avatar answered Oct 21 '22 23:10

joewright