Search code examples
codeignitercordovapostdata

Send post data from Phonegap (Cordova) to server


I have a form in my Cordova app:

<form id='transport' method='POST' action='' enctype='application/json'>
    <input type='hidden' id='data' name='data' value='' />
</form>

and I sent to server (pure js):

postdata = '{';
postdata += '"first_name": "Jon",';
postdata += '"last_name": "Snow"';
postdata += '}';

document.getElementById('data').value = postdata;
document.getElementById('transport').action = 'http://testserver.com/add_user/';
document.getElementById('transport').submit();

but the data variable is empty when received on server.

On server I'm using Codeigniter.

Works perfectly in a web scenario, why not in Cordova? I know there is not the cross-domain problem, and I have allowed all domains (*) in config.xml.

Thanks.


Solution

  • Fixed! Just remove te slash (/) at the end of the URL.

    This is because Codeigniter - with this slash - is expecting another parameter (due to its nature url-based) and if there is not, all the variables inside the controller (such as POST data) are null.

    So this:

    postdata = '{';
    postdata += '"first_name": "Jon",';
    postdata += '"last_name": "Snow"';
    postdata += '}';
    
    document.getElementById('data').value = postdata;
    document.getElementById('transport').action = 'http://testserver.com/add_user';
    document.getElementById('transport').submit();
    

    it's correct.