React axios数据类型问题

时间:2017-08-22 13:33:19

标签: javascript local-storage

我有以下提交方法:

submitForm(e) {
    e.preventDefault(); 
    var token = document.getElementsByTagName("meta")[0].getAttribute("content"); 
    var form = document.querySelector('.form'); 

    if(navigator.onLine && JSON.parse(localStorage.getItem('candidates'))) {

        axios({
            url: '/save-data',
            method: 'POST',
            contentType: 'application/json',
            data: { 
                "candidates": localStorage.getItem('candidates')
            },
            headers: { 
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': token
            }

        }).then(function(response) {
            console.log(response);
        })
        .catch(function(e){
            console.log(e);
        });
    }

    else {
        var singleCandidate = serialize(form, { hash: true }); 
        var fromStorage = localStorage.getItem("candidates");

        if (fromStorage) { 
            var parsedFromStorage = JSON.parse(fromStorage) 
            parsedFromStorage.push(singleCandidate) 
            localStorage.setItem("candidates", JSON.stringify(parsedFromStorage));
        } 
        else { 
            localStorage.setItem("candidates", JSON.stringify([singleCandidate]));
        } 
    }
}

它从本地存储加载数据,并在同时存在数据和连接时提交数据。 但是,有些内容不适用于以下内容:

            data: { 
                "candidates": localStorage.getItem('candidates')
            },

如果我这样做:

            data: {
                "candidates": [
                    {
                        "name": this.state.firstName, 
                        "email": this.state.email,
                        "university": this.state.university,
                        "degree": this.state.degree
                    }
                ]
            },

我得到了积极的回复,表单数据已提交。但是我必须提交这个值(localStorage.getItem('candidates')),当我这样做时,我得到以下内容:

enter image description here

“localStorage.getItem('candidate')”是否应该更改为其他格式?

0 个答案:

没有答案