SyntaxError:使用sessionStorage在位置0的JSON中出现意外的标记m

时间:2016-12-22 16:38:01

标签: javascript json session-storage

所以我正在做的是将表单操作发送到iframe,并使用JSON.stringify()存储表单输入数据,以防表单失败。下面是iframe用于确认页面的代码,以及第一个console.log()的数据。它似乎在JSON.parse部分失败了。

<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>

javascript的控制台输出如下。带有sessionStorage对象的第一个console.log输出和下面是JSON.parse方法的错误。我很困惑为什么它不能正常工作或可能导致问题的原因。

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)

更新:下面显示了如何存储对象:

// 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 个答案:

答案 0 :(得分:3)

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将以JSON格式包含您的数据。