Rajan Palaniya
Rajan Palaniya

Reputation: 69

JSON data Post to server in Titanium for Android / iOS

I'm trying post my JSON data to server but it fails when i submit the data. it gives me error code 403. i tried this method in ADT for android it worked. it's not working on titanium only.

Here's my code.

function postData() {

    Ti.API.info("JSON Data :" + JSONStringData);

    var url = "http://www.vrsweb.in/hotels/admin/ws/orderItems.php";
    var xhrpost = Titanium.Network.createHTTPClient();

    xhrpost.setTimeout(5000);
    xhrpost.open('POST', url);

    xhrpost.setRequestHeader("Content-Type", "application/json");
    xhrpost.setRequestHeader('charset', 'utf-8');
    xhrpost.setRequestHeader("enctype", "multipart/form-data");

    xhrpost.send(JSONStringData);

    xhrpost.onerror = function(e) {

        Ti.API.debug(e.error);
        if (platform == 'android') {
            var toast = Titanium.UI.createNotification({
                message : 'Cannot Connect to server please check your internet Connection!',
                duration : Titanium.UI.NOTIFICATION_DURATION_SHORT
            });
            toast.show();
        }
        if (platform == 'iphone') {
            alert('Cannot Connect to server please check your internet Connection!');
        }
        Ti.API.info("Status code :" + xhrpost.status);
    };
    xhrpost.onload = function() {
        if (xhr.status == 200) {
            Ti.API.info("XHR Status : " + xhrpost.status);
            data = JSON.parse(this.responseText);
            Ti.API.info("Response from server :" + data);
            if (platform == 'android') {
                var toast = Titanium.UI.createNotification({
                    message : 'Table booked Successfully!',
                    duration : Titanium.UI.NOTIFICATION_DURATION_SHORT
                });
                toast.show();
            }
            if (platform == 'iphone') {
                alert('Table booked Successfully!');
            }

        } else {

            Ti.API.info("XHR Status : " + xhrpost.status);
        }

    };

}

I don't understand what am i doing wrong.

And i am Collecting data through JSON Get Method from the server and adding it to array. Converting that array into JSON and posting it to server.

Here's how i get Order id from server and saving it to array.

aButton.addEventListener('click', function() {

            name = textUser.value;
            qty = textPass.value;
            price = textConfirmPass.value;
            type = textEmail.value;

            var url = "http://vrsweb.in/hotels/admin/ws/placeOrder.php?tName=" + id + "&wName=" + logName;

            var xhr = Titanium.Network.createHTTPClient();
            xhr.setTimeout(5000);
            //xhr.autoEncodeUrl = false;
            xhr.open('POST', url);
            xhr.setRequestHeader('User-Agent', 'My User Agent');

            xhr.send();
            xhr.onerror = function(e) {

                Ti.API.debug(e.error);
                var toast = Titanium.UI.createNotification({
                    message : 'Error! Can not book table! ',
                    duration : Titanium.UI.NOTIFICATION_DURATION_SHORT
                });
                toast.show();
                Ti.API.info("Status code :" + xhr.status);
            };
            xhr.onload = function() {
                if (xhr.status == 200) {
                    Ti.API.info("XHR Status : " + xhr.status);
                    oID = JSON.parse(this.responseText);
                    Ti.API.info("Response from server :" + oID);
                    var id = oID;
                    tempData = {
                        oId : id,
                        itemName : name,
                        qtyPerPlate : qty,
                        qtyPerKg : '0',
                        pricePerPlate : price,
                        pricePerKg : '0',
                        type : type
                    };

                    arrayOBJ.push(tempData);

                    orderData = {
                        Item : arrayOBJ
                    };

                    JSONStringData = JSON.stringify(orderData);
                    Ti.API.info("Data Added :" + orderData);
                    var toast = Titanium.UI.createNotification({
                        message : 'Added Data!',
                        duration : Titanium.UI.NOTIFICATION_DURATION_SHORT
                    });
                    toast.show();
                } else {

                    Ti.API.info("XHR Status : " + xhr.status);
                }

            };

            Ti.API.info("id :" + id);
            Ti.API.info("name :" + logName);

        });

Upvotes: 0

Views: 771

Answers (2)

Rajan Palaniya
Rajan Palaniya

Reputation: 69

I got it working. Here's what i changed.

xhrpost.setRequestHeader('User-Agent', 'My User Agent');
xhrpost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

Upvotes: 2

Mitul Bhalia
Mitul Bhalia

Reputation: 1217

There is no need to set charset and enctype so remove it and then try to call.

Upvotes: 0

Related Questions