SyntaxError: Unexpected token m in JSON at position 0 using sessionStorage

5.7k views Asked by At

So what i'm doing is sending a form action to an iframe, and storing the form input data using JSON.stringify() just in case the form fails. Below is the code the iframe is using for the confirmation page, and the data of first console.log(). It seems to be failing at the JSON.parse section.

<script type='text/javascript' src="/common/js/jquery-1.12.0-min.js"></script>
<script type='text/javascript'>
$(function(){
    console.log(sessionStorage);
    var formdata    = sessionStorage.getItem('formdata'),
        formid      = sessionStorage.getItem('formid'),
        dataParsed  = JSON.parse(formdata);//,
        //parent      = window.parent.document;

    // We need to check if error, and if failed, then change wizard to be correct
    console.log(formdata, formid);
});
</script>

The console output for the javascript is below. The first console.log outputs with the sessionStorage object and below is the error of the JSON.parse method. I'm confused as to why it's not working correctly or what might be causing the issue.

Storage.formdata: "merchant_defined_data1=0&merchant_defined_data2=0&merchant_defined_data3=1&merchant_defined_data4=0&merchant_defined_data5=monthly&merchant_defined_data6=0.00&merchant_defined_data7=0&bill_to_forename=John&bill_to_surname=Madrigal&bill_to_address_line1=+652+S+Walnut&bill_to_address_line2=+&bill_to_address_city=Cookeville&bill_to_address_state=TN&bill_to_address_country=US&bill_to_address_postal_code=38501&bill_to_email=jmadrigal%40tappublishing.com&amount=6.00&override_custom_receipt_page=http%3A%2F%2F74.43.119.28%2Fservice%2Faccount%2Fcsresponse&override_custom_cancel_page=http%3A%2F%2F74.43.119.28%2Fservice%2Faccount%2Fcsresponse
Storage.formid: "optionalPackagesForm"
length: 2


VM8596:1 Uncaught SyntaxError: Unexpected token m in JSON at position 0
at JSON.parse (<anonymous>)
at HTMLDocument.<anonymous> (csresponse:27)
at i (jquery-1.12.0-min.js:2)
at Object.fireWith [as resolveWith] (jquery-1.12.0-min.js:2)
at Function.ready (jquery-1.12.0-min.js:2)
at HTMLDocument.K (jquery-1.12.0-min.js:2)

UPDATE: Below shows how the object gets stored:

// Store the form data, and any other session data needed for failed transaction_id

var sigSelect = function(){
            var sigFields   = ['card_type','card_number','card_cvn','card_expiry_date','profile_id',
                            'locale','signed_date_time','reference_number', 'method',
                            'currency','signed_field_names','access_key','transaction_uuid',
                            'payment_method','signature','unsigned_field_names','transaction_type'],
                selectState = ':input:not(';

            $.each(sigFields, function(index, val){ selectState += '[name="'+ val +'"],'; });
            selectState = selectState.replace(/,\s*$/,"");
            return selectState += ')'
        },
inp     = $(sigSelect(), form).serialize();


sessionStorage.setItem('formdata', inp);
sessionStorage.setItem('formid', form.attr('id'));
1

There are 1 answers

0
Karthik VU On
var formData = "merchant_defined_data1=0&merchant_defined_data2=0&merchant_defined_data3=1&merchant_defined_data4=0&merchant_defined_data5=monthly&merchant_defined_data6=0.00&merchant_defined_data7=0&bill_to_forename=John&bill_to_surname=Madrigal&bill_to_address_line1=+652+S+Walnut&bill_to_address_line2=+&bill_to_address_city=Cookeville&bill_to_address_state=TN&bill_to_address_country=US&bill_to_address_postal_code=38501&bill_to_email=jmadrigal%40tappublishing.com&amount=6.00&override_custom_receipt_page=http%3A%2F%2F74.43.119.28%2Fservice%2Faccount%2Fcsresponse&override_custom_cancel_page=http%3A%2F%2F74.43.119.28%2Fservice%2Faccount%2Fcsresponse";
var dataJson = {};
(formData .split('&')).forEach(function(data){
  dataJson[data.split("=")[0]] = data.split("=")[1]; 
});

console.log(dataJson);

dataJson will contain your data in JSON Format.