0

I have a form I'm submitting via ajax POST, which on original submission shows as Array(74)[Object, Object, Object,...], but then if user loses connection, I store this info in a cookie to re-run on page load (connection re-established). However, when I go to process after reconnected/loaded, the cookie decides to store the data as [object Object],[object Object],[object Object],object Object..

Can someone explain what's happening here, and help me rectify?

Cheers

Example:

function submitOrder() {
        var postData = $('#form_name').serializeArray();
        var cookieName = "cookieName_" + $('#the_id').val();
        if(isonline()) {
            $.ajax({
                url: 'process_order.php',
                type: 'POST',
                data: postData,
                success: function () {
                    signatureSave(0);
                    window.location = "index_2.php";
                    alert("Work Order Submitted!");
                },
                failure: function () {
                    signatureSave(0);
                    setCookie(cookieName, postData);
                    window.location = "index_2.php";
                    alert("Work Order Pending - Please check connection!");
                },
                error: function () {
                    signatureSave(0);
                    setCookie(cookieName, postData);
                    window.location = "index_2.php";
                    alert("Work Order Pending - Please check connection!");
                }
            });
        }}

function setCookie(cname, cvalue) {
    var d = new Date();
    d.setTime(d.getTime() + (365*24*60*60*1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function checkForCookies() {
    if (getCookie("cookieName_").toString() != "") {
        var cookie = getCookie("cookieName_");
        var key = cookie.substring(0, cookie.indexOf("="));
        var value = cookie.substring(cookie.indexOf("=") + 1);
        console.log(value);
        $.ajax({
            url: 'process_order.php',
            type: 'POST',
            data: value,
            success: function() {
                alert("Pending order submitted...");
                deleteCookie(key);
                checkForCookies();
            }
        });
    }
}
Codeward
  • 41
  • 5
  • Think I may have fixed my own issue: setCookie: I JSON.stringify(cvalue); CheckForCookies: I JSON.parse(value); – Codeward May 08 '17 at 14:14
  • @kevin-b is correct, this is a duplicate of: http://stackoverflow.com/questions/4225030/jquery-save-json-data-object-in-cookie Searched for an answer, but didn't find one. – Codeward May 09 '17 at 12:16

1 Answers1

0

Issue Resolved. On setCookie(): JSON.stringify(cvalue); On checkForCookies(): JSON.parse(value);

I may have answered my own question, but I think this may help someone in future, so I'm just going to leave this here...

Codeward
  • 41
  • 5