4

我读过这个其他线程,但不好:Put data into JSON with Jquery

每当我尝试 JSON.stringify 一个对象数组时,我都会收到一条错误消息:

Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. 

这是我的 JS:

            var responseItems = [];

            var keynoteContainer = $('div.keynote-questions');
            var eventQuestionContainer = $('div.event-questions');
            var sessionContainer = $('div.session-questions');
            var eventId = $('#Evaluation-Event-Id').val();

            keynoteContainer.children().each(function (index, el) {
                var element = $(el);

                var id = "-1";
                var parentId = element.find('input[type=hidden]').val();
                var parentType = "Keynote";
                var responseValue = element.find('.response-item-slider').slider("option", "value");
                var responseText = "";

                var response = { "Id": id, "EventId": eventId, "ParentId": parentId, "ParentType": parentType, "ResponseValue": responseValue, "ResponseText": responseText };
                responseItems.push(response);
            });                    

            eventQuestionContainer.children().each(function (index, el) {
                var element = $(el);

                var id = "-1";
                var parentId = element.find('input[type=hidden]').val();
                var parentType = "EventQuestion";
                var responseValue = element.find('.response-item-slider').slider("option", "value");
                var responseText = element.find('textarea').val();

                var response = { "Id": id, "EventId": eventId, "ParentId": parentId, "ParentType": parentType, "ResponseValue": responseValue, "ResponseText": responseText };
                responseItems.push(response);
            });

            sessionContainer.children().each(function (index, el) {
                var element = $(el);

                var id = "-1";
                var parentId = element.find('input[type=hidden]').val();
                var parentType = "Session";
                var responseValue = element.find('.response-item-slider').slider("option", "value");
                var responseText = "";

                var response = { "Id": id, "EventId": eventId, "ParentId": parentId, "ParentType": parentType, "ResponseValue": responseValue, "ResponseText": responseText };
                responseItems.push(response);
            });

            responseItems = JSON.stringify(responseItems);

我试图记录所有内容,但它只在我对其进行字符串化的最后一行中断。

我怎样才能解决这个问题?任何建议或信息将不胜感激。

4

2 回答 2

4

我有同样的错误。就我而言,我在设置模型属性之一时忘记了 .val() 。在我尝试 JSON.stringify 模型之前没有错误

        this.model.set(
        {
            customername: this.$("#customername").val(),
            jobtitle: this.$("#jobtitle"),  //This was the line causing the error
            testimonialtext: this.$("#testimonialtext").val()
        });
于 2014-01-21T01:05:58.550 回答
2

我有同样的问题。就我而言,它已修复将我的麻烦变量转换为 toArray()。我的意思是这样的:
responseItems = responseItems.toArray();
我不知道为什么那个变量看起来像一个数组,但之后你可以调用它:
JSON.stringfy(responseItems);
没有问题。
PD:对不起我的英语我不是以英语为母语的人

于 2013-12-02T18:07:54.050 回答